Clear java 1ms solution


  • 0
    W

    public class Solution {
    public boolean isValidBST(TreeNode root) {
    if(root==null){
    return true;
    }
    if(root.left==null&&root.right==null){
    return true;
    }else if(root.left!=null&&root.right==null){
    if(isValidBST(root.left))
    return root.val>max(root.left);
    else
    return false;
    }else if(root.right!=null&&root.left==null){
    if(isValidBST(root.right))
    return root.val<min(root.right);
    else
    return false;
    }else{
    if(isValidBST(root.left)&&isValidBST(root.right))
    return (root.val<min(root.right))&&(root.val>max(root.left));
    else
    return false;
    }
    }
    int max(TreeNode root){
    if(root.right==null){
    return root.val;
    }
    return max(root.right);
    }
    int min(TreeNode root){
    if(root.left==null){
    return root.val;
    }
    return min(root.left);
    }
    }


Log in to reply
 

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