i) Nodes considered for the sum should be from parent to child.
ii) For a particular way, you can only consider nodes having the common path.
iii) Two ways are different from each other if they have an unequal number of nodes (for making sum equal to k) or at least one node is different between both the ways.
For the binary tree shown in the figure, the possible ways/paths are [8, 6, -4], [6, -3, 7], and [1, 9] for a sum of nodes equal to 10.
The first line contains an integer 'T' which denotes the number of test cases to be run. Then the test cases are as follows.
The first line of each test case contains an integer ‘K’ denoting the required sum.
The second line of each test case contains elements of the tree in the level order form. The line consists of values of nodes separated by a single space. In case a node is null, we take -1 in its place.
For example, the input for the tree depicted in the below image would be :
1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1
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 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6
Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)
Level 5 :
Left child of 7 = null (-1)
Right child of 7 = 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).
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 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
For each test case, print the number of ways to make the sum of nodes equal to k under the given conditions.
You do not need to print anything, it has already been taken care of. Just implement the given function.
1 <= T <= 10
0 <= N <= 500
-10^9 <= K <= 10^9
-100 <= data <= 100
Where ‘T’ is the number of test cases, and ‘N’ is the total number of nodes in the binary tree, ‘K’ is the required sum, and “data” is the value of the binary tree node. “data” equal to -1 represents a NULL value.
Time Limit: 1sec
The basic idea of this approach is to compute the sum from leaf to root node by applying pre-order traversal on the tree.
First, you have to declare a vector which will store nodes in a particular path. Let’s assume its name is “path”. You have to also declare a variable (Let’s assume the name “ans”) to store the answer (number of ways). Initialize “ans” with 0.
Now consider a recursive function which will be used to calculate the number of ways for the given parameters:
void helper(TreeNode *root, vector<int> &path, int K)
Where “root” denotes the root node of the constructed binary tree. “path” and ‘K’ denotes the vector for storing a particular path and required sum, respectively.
The basic idea of this approach is to compute the prefix sums for each node and check if there exists any other prefix sum (in a particular path) such that prefix sum till current node in addition with this prefix sum becomes equal to ‘K’.
Now consider a recursive function which will be used to calculate the number of ways for the given parameters:
void helper(TreeNode *root, int K, int prefix, int &totalCount, unordered_map<int,int> &count)
Where “root” denotes the root node of the constructed binary tree. ‘K’, “prefix”, “totalCount”, denotes the vector for storing a particular path and required sum, respectively.
Where,
“root” = root node of the constructed binary tree
‘K’ = required sum
“prefix” = variable for storing prefix sum
“totalCount” = variable to store answer (number of ways for the sum K)
“count” = used to store the pre-calculated prefix sums
Preorder Traversal
Inorder Traversal
Postorder Traversal
Height of Binary Tree
Locked Binary Tree