二叉搜索树的判断:可以根据二叉搜索树的定义,递归定义:左子树<当前节点<右子树
或者根据二叉搜索树的中序遍历是递增的
根据二叉树的中序遍历递增的解法
boolean flag = true;
public boolean isValidBST(TreeNode root) {
Stack<Integer> stack = new Stack<>();
inOrder(root, stack);
return flag;
}
private void inOrder(TreeNode root, Stack<Integer> stack) {
if(root == null) {
return;
}
inOrder(root.left, stack);
if(!stack.isEmpty()) {
int pre = stack.pop();
if(pre>=root.val) {
flag = false;
return;
}
}
stack.push(root.val);
inOrder(root.right, stack);
}