98-Validate-Binary-Search-Tree
0x0 题目详情
0x1 解题思路
0x2 代码实现
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
Integer pre=null;
public boolean isValidBST(TreeNode root) {
if(root==null){
return true;
}
return recur(root);
}
private boolean recur(TreeNode root){
if(root==null){
return true;
}
if(!recur(root.left)){
return false;
}
if(pre!=null && root.val<=pre){
return false;
}
pre=root.val;
if(!recur(root.right)){
return false;
}
return true;
}
}0x3 课后总结
Last updated