Any better answer??


  • -1
    X
    class Solution {
    public:
        bool isValidBST(TreeNode *root,int min = INT_MIN,int max = INT_MAX){
            if(!root) return true;
            if(root->val <= min || root->val>=max) return false;
            return isValidBST(root->left,min,root->val) && isValidBST(root->right,root->val, max);
        }
    };

  • 0
    J

    Your answer must be wrong if the node value is INT_MIN or INT_MAX.


  • 2
    J
    class Solution {
    public:
        bool isValidBST(TreeNode* root) {
            return validate(root, NULL, NULL);
        }
        bool validate(TreeNode* node, TreeNode* tmin, TreeNode* tmax) {
            if (node == NULL) return true;
            if (tmin != NULL && node->val <= tmin->val) return false;
            if (tmax != NULL && node->val >= tmax->val) return false;
            return validate(node->left, tmin, node) && validate(node->right, node, tmax);
        }
    };
    

    This solution is not tied to the integer type.


Log in to reply
 

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