FastPrepInorder Successor in a Binary Tree
Problems/Inorder Successor in a Binary Tree

Inorder Successor in a Binary Tree

Microsoft logoMicrosoft● HardINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: 💐 A big thank-you to the friend who shared this question! The report gives the goal of finding the inorder successor and the need for constant space. It leaves out the function signature, examples, the limits on values, and whether you may change the tree for a while. Our version is about 90% the same as the reported question.

You are given the root root of a non-empty binary tree and an integer targetValue. Every node has a unique non-negative integer value, and exactly one node has value targetValue.

Return the value of the node that appears immediately after the target node in an inorder traversal. An inorder traversal visits the left subtree, then the node, then the right subtree. If the target is the final node in that traversal, return -1.

Your algorithm must use O(1) auxiliary space. Do not use recursion, an explicit stack, or another auxiliary collection. You may temporarily redirect right-child pointers while traversing, but every pointer must be restored before the function returns.

Function

findInorderSuccessor(root: TreeNode, targetValue: int) → int

Examples

Example 1

root = [8,4,2,null,6,1,3]targetValue = 8return = 1
root
846213

The inorder sequence is [4, 6, 8, 1, 2, 3]. The node after 8 has value 1. This example also shows that the input is a general binary tree, not necessarily a binary search tree.

Example 2

root = [20,10,30,5,15,25,35]targetValue = 15return = 20
root
2010515302535

The inorder sequence is [5, 10, 15, 20, 25, 30, 35], so the successor of 15 is 20.

Example 3

root = [4,2,6,1,3,5,7]targetValue = 7return = -1
root
4213657

The target value 7 is the final value in inorder traversal, so it has no successor.

Constraints

  • The tree contains between 1 and 10^5 nodes.
  • 0 <= Node.val <= 10^9.
  • All node values are distinct.
  • targetValue equals the value of exactly one node in the tree.
  • The input is one finite, acyclic binary tree.
  • The solution must use O(1) auxiliary space without recursion, an explicit stack, or another auxiliary collection.
  • Every temporary pointer change must be restored before the function returns.

More Microsoft Onsite Interview questions

  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
  • Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
  • Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
  • Coin ChangeONSITE INTERVIEW · Seen Oct 2026
  • Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
  • Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026

Similar questions at other companies

  • Recover a Tree From Preorder TraversalGrammarly · PHONE SCREEN · ONSITE INTERVIEW
  • Colored Rooted TreeRubrik · ONSITE INTERVIEWRubrik interview questions
  • Serialize and Deserialize a Binary TreeAmazon · ONSITE INTERVIEW
  • Recursive Display SortFigma · PHONE SCREEN · ONSITE INTERVIEWFigma interview questions
  • Sum of Weighted Distances in a TreeGoogle · ONSITE INTERVIEWGoogle interview questions
  • Limit an Organization Tree's HeightRippling · PHONE SCREEN · ONSITE INTERVIEWRippling interview questions
  • Construct a Tree from Level-Order and Inorder TraversalsSalesforce · ONSITE INTERVIEWSalesforce interview questions
  • File Encryption Tree OptimizationDatabricks · PHONE SCREEN · ONSITE INTERVIEWDatabricks interview questions
  • N-Ary Tree Codec OperationsLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Binary Tree Maximum Path SumGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int findInorderSuccessor(TreeNode root, int targetValue) {
        // Write your code here.
    }
}
Checking account…