top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

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

+3 votes
389 views
               40

       30              60

   25      35      50      70

 12  27  32

Answer = -59

posted Oct 22, 2013 by Anuj Yadav

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+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;
}
answer Oct 22, 2013 by Vikas Upadhyay
Similar Questions
+7 votes

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.

+5 votes
      40
      /\
     20 60
     /\  \
   10 30  80
      /   /\
     25  70 90
           \
           75

longest path 
25 30 20 40 60 80 70 75 
Answer
    25 ----> 75
+6 votes
The input tree is as shown below
            40
            / \
        20      60
        / \       \
    10      30      80
      \            /  \ 
       15        70    90

Output: 15 30 60 80 90
...