C++ recursive solution 4 lines


  • -3
    M

    class Solution {

    public:

    bool verify(TreeNode* root,int low,int high,bool hlimit,bool llimit)
    {
        if(root==NULL) return true;
        if((hlimit && root->val>=high) ||(llimit && root->val<=low)) return false;
        return verify(root->left,low,root->val,true,llimit) && verify(root->right,root->val,high,hlimit,true);
    }
    bool isValidBST(TreeNode* root) {
        return verify(root,INT_MIN,INT_MAX,false,false);
    }
    

    };


  • 0
    H
    class Solution {
    public:
        bool verify(TreeNode* root,int low,int high,bool llimit,bool hlimit)
        {
            if(root==NULL) return true;
            if((hlimit && root->val>=high) ||(llimit && root->val<=low)) return false;
            return verify(root->left,low,root->val,llimit,true) && verify(root->right,root->val,high,true,hlimit);
        }
        bool isValidBST(TreeNode* root) {
            return verify(root,INT_MIN,INT_MAX,false,false);
        }
    };
    

Log in to reply
 

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