Inorder Successor in a Binary Tree
Updated
Problem statement
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) → intExamples
Example 1
root = [8,4,2,null,6,1,3]targetValue = 8return = 1The 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 = 20The 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 = -1The target value 7 is the final value in inorder traversal, so it has no successor.
Constraints
- The tree contains between
1and10^5nodes. 0 <= Node.val <= 10^9.- All node values are distinct.
targetValueequals 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)