Ch4.5: find the (in-order tranversely) successor of a node in BST.

来源:互联网 发布:iphone6s网络制式 编辑:程序博客网 时间:2024/05/12 11:47

There are 3 methods:

1. simply in order tranverse and get the ascending array, every next element is the successor of previous element.

2. It is Hawstein's method: There are 2 conditions: 

1st: have right child, go find the left least grand child.

2nd don't. go up to find the closest ancestor.


Here is whole code:


In order to verify, need to be careful, it is based on BST. So every simulation should have a BST structure. Random Tree will fail in this method.

0 0
原创粉丝点击