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:
/ \ / \
9 8 -4 7
Subtree with sum 7:
and one node 7.
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.
For each test case count the number of subtrees with given sum.
The task is to complete the function countSubtreesWithSumX() which check if there exists a subtree with sum x.
1 <= T <= 103
1 <= N <= 103
-103 <= Node Value <= 103
5 -10 L 5 3 R -10 9 L -10 8 R 3 -4 L 3 7 R
1 2 L 1 3 R
Testcase 1: Subtrees with sum 7 are [9, 8, -10] and .
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.