Given a node, to find the next largest value, there are two cases:
First: if the node has a right child. If it does, then t开发者_运维百科he next largest value is located at the left-most of the left subtree of its right child.
Second: if the node is a leaf, then the next largest value is in one of its parents. Which parent is it though?
Thanks!
Your first case is entirely correct. The second case, however, should be replaced with this:
2) If the node has no right child (note: this does not mean it is a leaf!), then you have to wonder up the (unique) path from the node to the root until you encouter a parent which is larger than its child. That parent is the next larger node. If no such parent exists, then the node we started with is the largest node in the tree.
精彩评论