Jave two line code


  • 1
    J
    public class Solution {
        public static boolean isValidBST(TreeNode root) {
            return root == null || dfs(root, Long.MAX_VALUE, Long.MIN_VALUE); }
        public static boolean dfs(TreeNode root, long max, long min) {
            return root == null || !(root.val >= max || root.val <= min) && dfs(root.left, root.val, min) && dfs(root.right, max, root.val);}
    

  • 0
    G

    This is really cool!
    Thank you for sharing.


  • 0
    A

    Hint:

    !(root.val >= max || root.val <= min)

    is the same as:

    min < root.val && root.val < max


Log in to reply
 

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