Simple C++ Solution

  • 0
    class Solution {
        TreeNode* inorderSuccessor(TreeNode* cur, TreeNode* p, TreeNode * ancestor = nullptr) {
            if (cur == nullptr || p == nullptr)
                return nullptr;
            if (p->val < cur->val)
                return inorderSuccessor(cur->left, p, cur);
            else if (p->val > cur->val)
                return inorderSuccessor(cur->right, p, ancestor);
            if (cur->right == nullptr)
                return ancestor;
            cur = cur->right;
                cur = cur->left;
            return cur;

Log in to reply

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