Generic Binary Tree Successor, Slow

  • 0

    class Solution {

        TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
            TreeNode* prev = nullptr;
            return rec(root, p, prev);
        TreeNode* rec(TreeNode* root, TreeNode* p, TreeNode* &prev)
            if(!root) return root;
            if(auto x = rec(root -> left, p, prev)) return x;
            if(prev && prev -> val == p -> val) return root;
            prev = root;
            if(auto x = rec(root -> right, p, prev)) return x;
            return nullptr;

Log in to reply

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