[英]Finding the size of n-ary tree
In C, I tried to find the size of n-ary tree like this. 在C语言中,我试图找到像这样的n元树的大小。 I know why this is wrong.
我知道为什么这是错误的。 but couldn't find out any way to return the size of n-ary tree.
但找不到任何方法来返回n元树的大小。
Can someone suggest a way to return the size of tree. 有人可以建议一种方法来返回树的大小。
int size(struct Node*root)
{
int sz=0;
if(root==NULL)
return 0;
else
{
for(int i=0;i<N;i++) sz=sz+1+size(root->child[i]);
}
return sz;
}
try this: 尝试这个:
int size(struct Node*root)
{
int sz=1;
if(!root) return 0;
for(int i=0;i<N;i++) sz+=size(root->child[i]);
return sz;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.