5 lines Java solution


  • 0
    W
    public class Solution {
        TreeNode prev = null;
        public boolean isValidBST(TreeNode root) {
            if(root==null) return true;
            if(!isValidBST(root.left)||(prev!=null&&root.val<=prev.val)) return false;
            prev = root;
            return isValidBST(root.right);
        }
    }

Log in to reply
 

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