Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

Given a Binary Tree. Find the difference between the sum of node values at even levels and the sum of node values at the odd levels.

**Example 1:**

Input:1 / \ 2 3Output:-4Explanation:sum at odd levels - sum at even levels = (1)-(2+3) = 1-5 = -4

**Example 2:**

Input:10 / \ 20 30 / \ 40 60Output:60Explanation:sum at odd levels - sum at even levels = (10+40+60) - (20+30) = 110 - 50 = 60

**Your Task: **

You dont need to read input or print anything. Complete the function **getLevelDiff()** which takes root node as input parameter and returns an integer.

**Expected Time Complexity: **O(N)

**Expected Auxiliary Space:** O(height of tree)

**Constraints:**

1 ≤ N ≤ 10^5

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Odd even level difference

...