Count Number of SubTrees having given Sum
Submissions: 12037   Accuracy:

50.13%

  Difficulty: Medium   Marks: 4

Given a binary tree containing N+1 with N edges nodes and an integer X. Your task is to complete the function countSubtreesWithSumX() that returns the count of the number of subtress having total node’s data sum equal to a value X.

 Example: For the tree given below:            

              5
            /    \
        -10     3
        /    \    /  \
      9     8  -4 7

Subtree with sum 7:
             -10
            /      \
          9        8

and one node 7.

Input:
First line of input contains number of testcases T. For each testcase, first line of input contains number of edges in the tree. Next line contains information as X Y L or X Y R which means Y is on the left of X or Y is on the right of X respectively. Last line contains sum.

Output:
For each test case count the number of subtrees with given sum.

Your Task:
The task is to complete the function countSubtreesWithSumX() which check if there exists a subtree with sum x.

Constraints:
1 <= T <= 103
1 <= N <= 103
-103 <= Node Value <= 103

Example:
Input:

2
6
5 -10 L 5 3 R -10 9 L -10 8 R 3 -4 L 3 7 R
7
2
1 2 L 1 3 R
5

Output:
2
0

Explanation:
Testcase 1:
Subtrees with sum 7 are [9, 8, -10] and [7].

 

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

Contributor: Harshit Sidhwa
Author: harshitsidhwa


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.