C++ Inorder Traversal, please help me what is the problem with this code, I think it is good.


  • 0
    Z
    class Solution {
    public:
        bool isValidBST(TreeNode* root) {
            if (root == NULL) {
                return true;
            }
            TreeNode * cur = root;
            int lastVal = INT_MIN;
            while (cur != NULL) {
                if (cur->left == NULL) {
                    if (cur->val <= lastVal) {
                        return false;
                    }
                    lastVal = cur->val;
                    cur = cur->right;
                } else {
                    TreeNode * temp = cur->left;
                    while (temp->right != NULL && temp->right != cur) {
                        temp = temp->right;
                    }
                    if (temp->right == NULL) {
                        temp->right = cur;
                        cur = cur->left;
                    } else {
                        if (cur->val <= lastVal) {
                            return false;
                        }
                        lastVal = cur->val;
                        temp->right = NULL;
                        cur = cur->right;
                    }
                }
            }
            return true;
        }
    };
    

Log in to reply
 

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