题目描述:



思路:
二叉搜索树的中序遍历是升序的。使用中序递归遍历,判断每个节点是不是比前面的节点大即可。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
double pre = -Double.MAX_VALUE;
public boolean isValidBST(TreeNode root) {
if(root==null)
return true;
boolean lbool = isValidBST(root.left);
if(pre>=root.val)
return false;
pre = root.val;
boolean rbool = isValidBST(root.right);
return lbool&&rbool;
}
}
网友评论