Java 2 Lines Solution


  • 0
    E
    /**
     * 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) {
            return chkTree(root, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
        }
        public boolean chkTree(TreeNode root, double min, double max){
            return root==null?true: root.val>min && root.val<max && chkTree(root.left, min, root.val) && chkTree(root.right, root.val, max);
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.