Difference between odd and even levels

Given a binary tree of integers. Find the difference between
the sum of nodes at odd level and sum of nodes at even level.

Note: Consider the level of root node as 1.

Constraints

1 <= Number of nodes in binary tree <= 100000
0 <= node values <= 10^9 

For Example

Input 1:
            1
          /   \
         2    3
        / \  / \
       4   5 6  7
      /
     8 
Output 1:
    10
    Sum of nodes at odd level = 23
    Sum of ndoes at even level = 13

Input 2:
            1
           /  \
          2    10
           \
            4
Output 2:
    -7
    Sum of nodes at odd level = 5
    Sum of ndoes at even level = 12
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Difference between odd and even levels on Interview Code Editor
Sign Up
to access hints and editorial solutions for Difference between odd and even levels

Discussion


Loading...
Click here to start solving coding interview questions