求多路树的高度
如何求多路树的高度? 如果我想找到二叉树的高度,我可以这样做:
int height(node *root) {
if (root == NULL)
return 0;
else
return max(height(root->left), height(root->right)) + 1;
}
但我不确定是否可以将类似的递归方法应用于多路树。
How do you find the height of a multi-way tree? If I wanted to find the height of a binary tree, I could do something like this:
int height(node *root) {
if (root == NULL)
return 0;
else
return max(height(root->left), height(root->right)) + 1;
}
But I am not sure if I can apply a similar recursive method to a multiway tree.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
一般情况是:
The general case would be:
这取决于子节点的存储方式。 让我们假设它们存储在向量中。 然后您可以使用以下内容来计算它们的高度。
This depends on how the child nodes are stored. Lets assume for a second that they are stored in a vector. You could then use the following to calculate their height.
就其价值而言(几乎没有价值),这个问题在 SML 等纯函数式语言中可以完美呈现:
For what it's worth (almost nothing), this problem renders beautifully in pure-functional languages like SML:
如果非空:
if non-null:
不是'1+从(当前)根节点的任何子节点开始的子树的最大高度'吗?
请注意,二叉树只是多路树的一种特殊情况,其中子节点已知为左子节点和右子节点。 如果根节点指针为空,则结果为零。
Isn't it '1 + maximum height of sub-tree starting from any of the child nodes of the (current) root node'?
Note that the binary tree is just a special case of the multi-way tree where the child nodes are known to be the left child and the right child. The result is zero if the root node pointer is null.