Find number of leaf nodes in a binary tree having depth D using array implementation [closed]
C code to find the number of leaf nodes in a tree with depth d. Hint is to use array implementation of binary tree.
ignoring the hint...
int FindNumLeafs(Tree t)
{
if(t == null)
{
return 0;
}
if(t.LeftSon == null && t.RightSon == null)
{
return 1;
}
return FindNumLeafs(t.LeftSon) + FindNumLeafs(t.RightSon);
}
Height of a balanced binary tree is given by log2(n). Therefore, leaf nodes = 2^d.
精彩评论