C++ Very Clean Solution Using LONG_MIN & LONG_MAX


  • 0
    L

    Not sure the drawbacks of using LONG_MIN and LONG_MAX but this does solve the problem and it's very clean, quick and understandable.

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

Log in to reply
 

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