For example for a binary tree like this:
50 20 100 30 40 25 45 32 23 21 22 11
The inorder successor of node 30 is 20.
OK I thought it would be okay just to put the interview question. I didn't expect people to answer it.
But since people post the link to the inorder successor of a Binary SEARCH Tree's link here, I have to say that the problem is not BST but Binary Tree.
The idea is:
If the target node has a right child, then the answer will be the left most child for the right subtree.
If the target node does not have a child, then the answer will be the parent of this node.
If it is the last node in the Inorder Traversal, return null.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.