二叉树的中心
我们如何找到二叉树的中心? 什么是最有效的算法。虽然二叉树的中心将是与树的直径相对应的路径的中点。我们可以在不知道路径的情况下找到树的直径,是否有类似的技术可以找到二叉树的中心?
How can we find center of a binary tree?
What shall be the most efficient algorithm. Though center of binary tree will be the mid point of the path corresponding to the diameter of tree. We can find the diameter of tree without actually knowing the path, is there any similar technique for finding center of binary tree?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您知道直径:D
并且知道树的最大深度:M
那么您的中心将位于最深路径上的第 (M-(D/2)) 个节点(从根开始)。(可能是M - (D-1)/2 取决于奇偶校验,您需要自行检查)
如果从根到叶有超过 1 条具有 M 个节点的路径,则中心是根。 (仅当最长路径穿过根时才正确)
编辑:
回答您的评论。
如果没有贯穿根部。让我们取直径上的第 D/2 个节点,它仍然位于直径路径的最长一侧(在所有情况下都是从根到叶的最长路径)。因此MD/2仍然从根本上代表了这一点。
从根开始取 MD/2n 与从最长路径的叶子开始取 D/2n 是一样的。
我说得够清楚吗?您可能只想绘制它来检查它。
If you know the diameter : D
and you know the max depth of the tree : M
then your center will be at the (M-(D/2)) th node(from the root) on the deepest path.(it might be M - (D-1)/2 depending on parity, you need to check yourself)
If you have more than on 1 paths from root to leaf with M nodes then the center is the root. (only true when the longest path goes through the root)
EDIT:
To answer your remark.
if it doesn't go through the root. Let's take the D/2th node on the diameter it will still be on the longest side of the diameter path (wich is in all the cases the longest path from root to leaf). and therefore M-D/2 still represent this point from the root.
Taking M-D/2nth from the root is the same as talking D/2nth from the leaf of the longest path.
Am I clear enough ? You might just want to draw it to check it .
如果您使用递归方法,通过使用树的高度计算直径 (在此处查看此网站)。
例如,调整我上面发布的链接中的线性时间直径函数,以便您还可以收集您访问过的节点的列表,而不仅仅是距离信息。在每次递归调用中,您将选择与较长遍历距离相关的列表。代表树直径的列表的中间将是树的“中心”。
您的设置如下所示:
现在,该函数在
full_path
结构成员中返回一系列指针,可用于循环并查找将成为该路径“中心”的中间节点。树。PS 我知道利用复制函数并不是最快的方法,但我想更清楚,而不是制作更快但有太多指针旋转的东西。
You could calculate this in linear time O(N) by storing a list of the nodes that you have traversed if you are using a recursive method where you calculate the diameter by using the height of the tree (see this website here).
For instance, adapt the linear-time
diameter
function at the link I posted above so that you are also collecting a list of the nodes you have visited, and not just distance information. On each recursive call, you would select the list that went along with the longer traversed distance. The middle of the list that represented the diameter of the tree would be the "center" of the tree.Your setup would look like the following:
Now the function returns a series of pointers in the
full_path
structure member that can be used to cycle though and find the middle-node which will be the "center" of the tree.P.S. I understand that utilizing copying functions is not the fastest approach, but I wanted to be clearer rather than make something that was faster but had too much pointer-twiddling.