It was Todd’s Birthday, So Bojack decided to give Todd a Binary tree with ‘N’ nodes. But the binary tree was too big to keep in his house, so Bojack decided to give exactly three nodes from that tree such that the sum of these three numbers equals ‘X’. Can you help Bojack determine if it is possible to make a sum equal to ‘X’?
For Example :X = 11
The sum of nodes 7, 3, 1 is 11. So we will return True.
The first line contains a single integer ‘T’ denoting the number of test cases.
The first line of each test case contains the elements of the binary tree in the level order form separated by a single space.
The second line of the test case contains an integer denoting ‘X’.
If any node does not have a left or right child, take -1 in its place. Refer to the example below.
1
2 3
-1 4 -1 -1
5 -1
-1 -1
Explanation :
Level 1 :
The root node of the tree is 1
Level 2 :
Left child of 1 = 2
Right child of 1 = 3
Level 3 :
Left child of 2 = null (-1)
Right child of 2 = 4
Left child of 3 = null (-1)
Right child of 3 = null (-1)
Level 4 :
Left child of 4 = 5
Right child of 4 = null (-1)
Level 5 :
Left child of 5 = null (-1)
Right child of 5 = null (-1)
The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.
The input ends when all nodes at the last level are null (-1).
Note: The above format was just to provide clarity on how the input is formed for a given tree.
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:
1 2 3 -1 4 -1 -1 5 -1 -1 -1
Output Format :
For each test case, print a boolean True or False.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function and return the answer.
1 <= T <= 100
1 <= N <= 3000
1 <= NodeVal <= 1000
1 <= X <= 1000
Time Limit: 1sec
2
3 1 2 -1 -1 -1 -1
6
1 2 3 4 -1 -1 -1 -1 -1
3
True
False
For the First Test Case, The three nodes 1, 2, 3 have a sum equal to 6. Hence, the answer is true.

For the second Test Case, No combination of any three nodes results in a sum equal to 3. Hence, the answer is False.
2
1 8 1 3 4 -1 -1 -1 -1 -1 -1
8
1 1 1 -1 -1 -1 -1
2
True
False
Try to find the value of every node and check if the sum of any three nodes is equal to X.
The Approach is simple. We will find the value of every node using dfs and then increase the count of every value. After completing dfs, We will loop through the list and, for a given value, check if there are two other elements present such that the sum of these three numbers is X.
The steps are as follows :
O(X^3). Where X is the value given in the problem statement.
We are iterating through the list in nested loops from 1 to X, Hence the time complexity is O(X^3).
O(X), Where X is the value given in the problem statement
We are using a list arr of size X to store the count of nodes, Hence the space complexity is O(X).