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.

narahc321 | 170 |

agness | 163 |

lonecoder | 153 |

2016uec1342 | 142 |

HuHu | 140 |

lonecoder | 1088 |

nikhil_sojan | 1082 |

KshatriyaYash | 1066 |

mazumderrohit8 | 928 |

SumitSingh27 | 832 |

blackshadows | 5327 |

Ibrahim Nash | 5215 |

akhayrutdinov | 5111 |

mb1973 | 4504 |

Quandray | 4444 |

Login to report an issue on this page.