Simple java solution


  • 2
    /**
     * 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;
            return check(root, Long.MIN_VALUE, Long.MAX_VALUE);
        }
        
        public boolean check(TreeNode node, long min, long max){
            
            if(node.val>min && node.val<max){
                if(node.left!=null){
                    if(!check(node.left, min, node.val)){
                        return false;
                    }
                }
                if(node.right!=null){
                    if(!check(node.right, node.val, max)){
                        return false;
                    }
                }
            }else{
                return false;
            }
            return true;
        }
    }

Log in to reply
 

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