P14322 [GeeksforGeeks] Odd even level difference
普及-
题目描述

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 3
Output: -4
Explanation:
sum at odd levels - sum at even levels
= (1)-(2+3) = 1-5 = -4
Example 2:
Input:
10
/ \
20 30
/ \
40 60
Output: 60
Explanation:
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

输入格式

见题面

输出格式

见题面

样例输入
1 / \ 2 3
样例输出
-4