My C++ 9ms non recursive solution


  • 0
    U

    class Solution {
    public:

    bool isValidBST(TreeNode* root) {
        if (!root)
            return true;
        vector<TreeNode *> stack;
        TreeNode *current = root, *last = NULL;
        while (!stack.empty() || current)
        {   
            if (current)
            {   
                stack.push_back(current);
                current = current->left;
            }   
            else
            {   
                current = *stack.rbegin();
                stack.pop_back();
                if (last && last->val >= current->val)
                    return false;
                last = current;
                current = current->right;
            }
        }
        return true;
    }
    

    };


Log in to reply
 

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