# Find the subtraction of sum of nodes on even level and odd level.

131 views
``````               40

30              60

25      35      50      70

12  27  32
``````

posted Oct 22, 2013

+1 vote
``````void sumlevel(node *root, int *esum, int *osum, int h)
{
if (root == NULL)
return 0;
else
{
if(h %2 == 0)
*esum = *esum + root->val;
else
*osum = *osum + root->val;
sumlevel(root->left, esum, osum, h+1);
sumlevel(root->right, esum, osum, h+1);
}
}
int diff_of_level(node *root)
{
int esum = 0, osum = 0;
int h;
sumlevel(root, &esum, &osum, 1);
return esum - osum;
}
``````
Similar Questions

A binary tree is given and two nodes of this tree is given, we have to find out the algorithm/code for lowest common ancestor of the two given nodes. Common ancestor are the nodes which can be reached from two nodes by following the parent links. Lowest common ancestor is the common ancestor which has the highest depth.

``````      40
/\
20 60
/\  \
10 30  80
/   /\
25  70 90
\
75

longest path
25 30 20 40 60 80 70 75
25 ----> 75
``````
``````The input tree is as shown below