Timer is Running

Sum Tree
Submissions: 56251   Accuracy:


  Difficulty: Easy   Marks: 2
Associated Course(s):   Amazon SDE Test Series

Write a function that returns true if the given Binary Tree of size N is SumTree else return false. A SumTree is a Binary Tree in which value of each node x is equal to sum of nodes present in its left subtree and right subtree . An empty tree is SumTree and sum of an empty tree can be considered as 0. A leaf node is also considered as SumTree.

Following is an example of SumTree.

        /    \
      10      3
    /   \   /   \ 
   4     6  1    2


The 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: 

  1. The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.

  2. For example:

    For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N

Print "1" if the given tree is SumTree else print "0".

Your Task:
You don't need to take input. Just complete the function isSumTree() that takes root node as parameter and returns true, if the tree is SumTree else returns false.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Height of the Tree).

1 <=T<=100
1 <= N <= 104


3 1 2
10 20 30 10 10


Note:The Input/Ouput format and Example given are used for 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 stdin/console. The task is to complete the function specified, and not to write the full code.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Amit Khandelwal, Harshit Sidhwa
Author: kartik

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.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.