开发者

Next Largest Value in a Binary Tree Value [closed]

开发者 https://www.devze.com 2023-04-11 15:11 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号