Validate Binary Search Tree
2
/ \
1 3 1
/ \
2 3Last updated
2
/ \
1 3 1
/ \
2 3Last updated
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
return helper(root, Long.MAX_VALUE, Long.MIN_VALUE);
}
boolean helper(TreeNode root, long max, long min){
if(root == null){
return true;
}
if(root.val >= max || root.val <= min){
return false;
}
return helper(root.left, root.val, min) && helper(root.right, max, root.val);
}
}