C++ solution with the iterative inorder traversal.


  • 2
    Y
    bool isValidBST(TreeNode* root) {
        //gonna use the inordertraversal
        stack<TreeNode*> st;
        signed long long int min = LONG_LONG_MIN;
        while(!st.empty()|| root != NULL) {
            if (root != NULL) {
                st.push(root);
                root = root->left;
            } else {
                root = st.top();
                st.pop();
                if (min < root->val)
                    min = root->val;
                else
                    return false;
                root = root->right;
            }
            
        }
        
        return true;
    }

Log in to reply
 

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