C++ easily understand recursion method


  • -1
    D
    class Solution {
    public:
        bool isValidBST(TreeNode* root) {
            if(!root) return true;
            TreeNode* tmp;
            if(!root->left && !root->right) return true;
            if(root->left){
                tmp=root->left;
                while(tmp->right) tmp = tmp->right;
                if(tmp->val>=root->val) return false;
            }
            if(root->right){
                tmp=root->right;
                while(tmp->left) tmp=tmp->left;
                if(tmp->val<=root->val) return false;
            }
            if(root->left && root->right) return isValidBST(root->left)&&isValidBST(root->right);
            else if(root->left) return isValidBST(root->left);
            else return isValidBST(root->right);
        }
    };

Log in to reply
 

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