C++ with function overload to deal with INT_MAX, INT_MIN

  • 0

    Update the max and min until we can.

    class Solution {
        bool isValidBST(TreeNode* root) {
            if(!root) return true;
            return isValid(root->val, root->right) && isValid(root->left, root->val);
        bool isValid(int min, TreeNode* node) {
            if(!node) return true;
            if(node->val <= min) return false;
            return isValid(node->val, node->right) && isValid(min, node->left, node->val);
        bool isValid(TreeNode* node, int max) {
            if(!node) return true;
            if(node->val >= max) return false;
            return isValid(node->left, node->val) && isValid(node->val, node->right, max);
        bool isValid(int min, TreeNode* node, int max) {
            if(!node) return true;
            if(node->val >= max || node->val <= min) return false;
            return isValid(min, node->left, node->val) && isValid(node->val, node->right, max);

Log in to reply

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