Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- public class Solution {
- public boolean isValidBST(TreeNode root) {
- if(root == null) return true;
- if(root.right == null && root.left == null){
- return true;
- }
- return isValidHelper(root, Double.MAX_VALUE, Double.NEGATIVE_INFINITY);
- }
- public boolean isValidHelper(TreeNode root, double max, double min){
- if(root == null) return true;
- if(root.val <= min || root.val >= max){
- return false;
- }
- return isValidHelper(root.left, root.val, min) && isValidHelper(root.right, max, root.val);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement