Problem of the day
You are given the root of a binary tree. Print the values of the tree in the following way:
i. Print the values of nodes of the last level in this way: print the leftmost value then the rightmost value and continue until all the values of nodes of the last level are printed.
ii. Do the same for the second last level then the third last level until the first level.
There are total ‘N’ nodes, nodes are numbered from 0 to ‘N’-1. The values of nodes are given by an array ‘NUMS’, Where ‘NUMS[ i ]’ = the value of the ‘i’th node. You are also given the level order traversal of the tree in an array ‘SEQUENCE’. ‘SEQUENCE[ i ]’ = -1 denotes a null node.
Example:Input: ‘N’ = 3, ‘NUMS’ = [1, 2, 3], ‘SEQUENCE’ = [0, 1, 2, -1, -1, -1, -1].
Output: [2, 3, 1]
Printing the last level as the leftmost value then the rightmost value we get = 2, 3
Printing the 2nd last level we get = 1
Hence, the final array is: [2, 3, 1].
The first line will contain the integer 'T', denoting the number of test cases.
The first line of each test case contains one integers ‘N’ where ‘N’ denotes the length of the array ‘NUMS’.
The second line of each test case contains ‘N’ integers.
The third line of each test case contains ‘2*N+1’ integers.
Output format :
For each test case, you don’t need to print anything just return the resultant array.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^5
Sum of ‘N’ <= 10^5
1 <= NUMS[i] <= 10^9
Time Limit: 1 sec
1
6
5 3 9 4 2 1
0 1 2 3 4 -1 5 -1 -1 -1 -1 -1 -1
4 1 2 3 9 5
For the first case:
The tree generated from the edges is:
Starting from the last level printing the leftmost value then the rightmost value we get 4 1 2 3 9 5.
So the final output is [4, 1, 2, 3, 9, 5].
2
3
1 2 3
0 1 2 -1 -1 -1 -1
2
5 11
0 -1 1 -1 -1
2 3 1
11 5
Can you think of any way to store the values of nodes level-wise and then print it?
In this approach, We can do a bfs to store the values of the nodes level-wise in a matrix in which the row represents a level and the column represents the values in that level. Since the bfs explore the nodes from the first level then the second level and so on. We need to reverse the rows of the matrix. After reversing the matrix iterate over all the rows and print the values as per the rule.
The steps are as follows:-
Function leftRightPrint(int n, int m, vector<int>& nums, vector<vector<int>>& edges):
O( N ), Where ‘N’ denotes the number of nodes in the tree.
BFS visits each node only once hence the time complexity for BFS is O( N ). Printing the values of the nodes level-wise also visits each node only once. Hence the time complexity to print is also O( N ).
Hence the time complexity is O( N ).
O( N ), Where ‘N’ is the number of nodes.
Since the tree is a binary tree so adjacency list for each node will be of size at most 2. Hence the space complexity for the ‘TREE’ matrix is O( N ). The space complexity for ‘levelValues’ and ‘ANS’ array is also O( N ) because it stores each node only once.
Hence space complexity is O( N ).