Timer is Running

Leftmost and rightmost nodes of binary tree
Submissions: 7799   Accuracy:


  Difficulty: Medium   Marks: 4

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.


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: 

  1. 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.

  2. For example:

    For the above tree, the string will be: 1 2 3 N N 4 6 N 5 N N 7 N

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).

1<=data of each node<=105


1 3 2
10 20 30 40 60

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.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Ayush Govil
Author: Ayush Govil 1

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.

Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.