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)
      return 0;
      return root->data;
   return root->data + Sum(root->left, n-1) + Sum(root->right, n-1);

No comments:

Post a Comment