题解 | #判断是不是二叉搜索树#
判断是不是二叉搜索树
http://www.nowcoder.com/practice/a69242b39baf45dea217815c7dedb52b
二叉搜索树中序遍历是有序的,所以中序遍历中,如果pre结点的值大于当前结点的值则说明该树不是二叉搜索树。
public class Solution {
TreeNode pre = null;
public boolean isValidBST (TreeNode root) {
if(root == null){
return true;
}
boolean left = isValidBST(root.left);
if(pre != null && pre.val > root.val){
return false;
}
pre = root;
boolean right = isValidBST(root.right);
return left && right;
}
}
