285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
Note: If the given node has no in-order successor in the tree, return null.
Example 1:
Input: root = [2,1,3], p = 1
2
/ \
1 3
Output: 2Example 2:
Input: root = [5,3,6,2,4,null,null,1], p = 6
5
/ \
3 6
/ \
2 4
/
1
Output: nullNaive implementation:
Iterative (Fast):
Recursive (Faster):
做 recursive 的 tree 题时,想象把每一个大 🍰 切分成可用相同方法处理的小 🍰;每次只思考小 🍰 的处理方法即可写出 recursive 的步骤。最终停止在 node 为 None 这个最小的 🍰 上。
Last updated