开发者

Find number of leaf nodes in a binary tree having depth D using array implementation [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center开发者_StackOverflow. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜