Wednesday, April 16, 2014

In A Binary Tree, Find sum of all nodes upto certain level

Given a binary tree, find sum of all the nodes upto certain level.

Root is at level 0.
All the children of root are at level 1.
All the grand children of root are at level 2.
and so on.

Given a level n, find some of all the nodes that are at level n or less.

int Sum(Node root, level n)
{
   if(root==NULL)
      return 0;
   if(n==0)
      return root->data;
   return root->data + Sum(root->left, n-1) + Sum(root->right, n-1);
}

1 comment:

  1. Once your account is created, you'll be logged-in to this account. There are sure restrictions concerning your place of residence. You can see the record of restricted nations in GamingZion's evaluate 1xbet about Jackpot City Casino.

    ReplyDelete