Accepted 4ms straightforward Java solution


  • 0
    I

    public class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {

        TreeNode parent = null;
        while(root != null){
            if(root == p){
                if(root.right != null){
                    root = root.right;
                    while(root.left != null){
                        root = root.left;
                    }
                    return root;
                }else{
                    return parent;
                }
            }else if(root.val > p.val){
                parent = root;
                root = root.left;
            }else{
                root = root.right;
            }
        }
        return root;
    }
    

    }


Log in to reply
 

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