Simple c++ recursive solution 12ms


  • 0
    C

    Simple solution using recursive approach.

    class Solution {
    private:
        inline bool validate(TreeNode* node, long min, long max) {
            if (!node) return true;
            long val = node->val;
            if (val >= max || val <= min) {
                return false;
            }
            return validate(node->left, min, val) && validate(node->right, val, max);
        }
    public:
        bool isValidBST(TreeNode* root) {
            return validate(root, LONG_MIN, LONG_MAX);
        }
    };

  • 0
    C

    "Why rely on LLONG_MIN? Don't you think if you are in a true interview, the next question the interviewer will ask, "okay I have changed my input type to long" then? I am sorry but using long limit to such question is like taking shortcut to just get your answer accepted and won't help you in real interview.

    commented Jun 4, 2015 by namangoyal"

    Indeed using LONG_MIN LONG_MAX is taking shortcuts.. need to rethink.


Log in to reply
 

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