Solution using long for Integer.MIN_VALUE case. How to avoid using global variable here?


  • 0
    B
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        private long val;
        public boolean isValid(TreeNode root) {
            
            
            if(root == null)return true;
            
            Boolean v1 = isValid(root.left);
          //  System.out.println(val);
            if(v1)
            {
                if(root.val <= val)return false;
                val=root.val;
                
                
            }
            else
            {
                return false;
            }
            
            return isValid(root.right);
                
        }
        
        public boolean isValidBST(TreeNode root) {
          val = (long)Integer.MIN_VALUE-1;
          
          return isValid(root); 
        }
    }
    

Log in to reply
 

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