Share my 9 lines Java iterative solution


  • 0
    M
        public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
            TreeNode prev = null;
            while(root != p) 
                root = root.val > p.val? (prev = root).left : root.right;
            if( (root = root.right) != null) {
                while(root.left != null) 
                    root = root.left;
                return root;
            }
            return prev;
        }
    

Log in to reply
 

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