My simple c++ solution


  • 0
    K
    class Solution {
    public:
    void helper(TreeNode* root, TreeNode * p, TreeNode * &res)
    {
        if(!root)return;
        if(root->val==p->val)
        {
            if(root->right)
            {
                TreeNode*tmp=root->right;
                while(tmp->left)tmp=tmp->left;
                res=tmp;
            }
            return;
        }
        if(root->val > p->val){
            res=root;
            helper(root->left, p, res);
        }
        else helper(root->right, p, res);
    }
    TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
        TreeNode * res=NULL;
        helper(root, p, res);
        return res;
    }
    

    };


Log in to reply
 

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