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 <= 10^{3}

1 <= N <= 10^{3}

-10^{3} <= Node Value <= 10^{3}

**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].

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.

geetyadav | 82 |

hari32147 | 76 |

AdityaRaj9 | 75 |

yashrana1 | 74 |

amitpandey19 | 67 |

abducodes | 850 |

harshtyagi36 | 417 |

vamsikrishna55 | 323 |

sandipbhaumik | 321 |

Akhil varma | 314 |

blackshadows | 5331 |

Ibrahim Nash | 5219 |

akhayrutdinov | 5111 |

mb1973 | 4929 |

Quandray | 4567 |

Login to report an issue on this page.