/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
//记录前一个节点
TreeNode pre;
public boolean isValidBST(TreeNode root) {
//整体思路是利用了 二叉搜索树中序遍历递增的性质,如果前一个节点的值大于当前节点值,就fasle
if(root==null){
return true;
}
//左
boolean left = isValidBST(root.left);
//中
if(pre!=null && pre.val>=root.val){
return false;
}
//更新pre
pre = root;
//右
boolean right = isValidBST(root.right);
return left && right;
}
}