7 line concise solution

  • 0
    boolean find = false;
        public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
            if(root == null) return null;
            TreeNode temp = inorderSuccessor(root.left, p);
            if(temp != null) return temp;
            else if(find) return root;
            if(root.val == p.val) find = true;
            return inorderSuccessor(root.right, p);

Log in to reply

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