开发者

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

开发者 https://www.devze.com 2023-02-10 16:27 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
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

精彩评论

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