c - Finding the size of n-ary tree -
in c, tried find size of n-ary tree this. know why wrong. couldn't find out way return size of n-ary tree.
can suggest way return 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; }
Comments
Post a Comment