Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
If the given node has no in-order successor in the tree, return null.
Note
It’s guaranteed p is one node in the given tree. (You can directly compare the memory address to find p)
Example
No.1
Input: {1,#,2}, node with value 1
Output: 2
Explanation:
1 | 1 |
No.2
Input: {2,1,3}, node with value 1
Output: 2
Explanation:
1 | 2 |
Challenge
O(h), where h is the height of the BST.
Code
1 | public class TreeNode { |
1 | public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { |