Given a binary tree, complete the function that returns true if the tree satisfies the following property:
For every node, data value must be equal to the sum of data values in left and right children. Consider data value as 0 for NULL child. Also, leaves are considered to follow the property.
Input Format:
First line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below:
The values in the string are in the order of level order traversal of the tree where, numbers denotes node values, and a character “N” denotes NULL child.
For example:
For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N
Output Format:
Return 1 if the given tree satisfies the given property else return 0.
Your Task:
This is a function problem. You need to only complete the given function isSumProperty() that returns 0 or 1. The printing is done by the driver code.
Constraints:
1 <= T <= 100
1 <= N <= 100
1 <= Data on nodes <= 1000
Example:
Input:
2
10 10
1 4 3 5 N
Output:
1
0
Explanation:
Testcase 1: Tree is like:
10
/
10
Here, every node is sum of its left and right child.
Note: The Input/Output format and Example given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from the stdin/console. The task is to complete the function specified, and not to write the full code.
If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.
tushki | 198 |
namansharmacse17 | 110 |
TusharMittal3 | 70 |
adkeaesph | 57 |
deepakchoudhary11 | 48 |
ARUN_PUNDIR | 720 |
manishgupta8497 | 623 |
AMAN__JAIN | 596 |
Abhimanyu_kumar | 562 |
Bicky_Shokeen | 532 |
blackshadows | 5335 |
Ibrahim Nash | 5223 |
akhayrutdinov | 5111 |
mb1973 | 4929 |
Quandray | 4584 |