Share my C++ solution, easy to understand


  • 0
    V
    class Solution {
    public:
        TreeNode* pre = NULL;
        
        bool isValidBST(TreeNode* root) {
            return is_valid(root);
        }
        
        bool is_valid(TreeNode* root)
        {
            if (root == NULL)
                return true;
            
            if (!is_valid(root->left))
                return false;
            
            if (pre != NULL && root->val <= pre->val)
                return false;
            
            pre = root;
            
            return is_valid(root->right);
        }
    };
    

Log in to reply
 

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