Given a Binary Tree, Print the corner nodes at each level. The node at the leftmost and the node at the rightmost of each level.
Input:
The first line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below:
The values in the string are in the order of level order traversal of the tree where, numbers denote node values, and a character “N” denotes NULL child.
For example:
For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N
Output:
Print the corner nodes ( nodes at the leftmost and nodes at the rightmost) at each level.
Your Task:
This is a function problem. You don't have to take input. Just complete the function printCorner() that takes a node as parameter and prints the corner. The new line is automatically appended by the driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(Number of nodes at any level).
Constraints:
1<=T<=30
1<=data of each node<=10^{5}
1<=n<=10^{5}
Example:
Input:
2
1 3 2
10 20 30 40 60
Output:
1 3 2
10 20 30 40 60
Note: The Input/Output format and Examples given are used for the system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.
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.
abnandi04 | 801 |
KshamaGupta | 412 |
srivastavad270 | 223 |
bhavyapratap56 | 206 |
anshusingh20112001 | 181 |
abnandi04 | 776 |
KshamaGupta | 296 |
srivastavad270 | 223 |
bhavyapratap56 | 206 |
afreensartaj131 | 170 |
blackshadows | 5362 |
Ibrahim Nash | 5244 |
akhayrutdinov | 5111 |
mb1973 | 4976 |
Quandray | 4748 |