Inorder Successor in BST(tree traverse)

Given a binary search tree (See Definition) 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, returnnull.

Example

Given tree =[2,1]and node =1:

  2
 /
1

return node2.

Given tree =[2,1,3]and node =2:

  2
 / \
1   3

return node3.

Challenge

O(h), where h is the height of the BST.

Notice

It's guaranteed_p_is one node in the given tree. (You can directly compare the memory address to find p)

分析

就是中序遍历加条件

递归

这里相当于二分,找到第一个root.val>p.val

Last updated

Was this helpful?