Leftmost and rightmost nodes of binary tree
Medium Accuracy: 50.0% Submissions: 10381 Points: 4

Given a Binary Tree of size N, Print the corner nodes ie- the node at the leftmost and rightmost of each level.

Example 1:

Input :
         1
       /  \
     2      3
    / \    / \
   4   5  6   7    

Output: 1 2 3 4 7

Explanation:
Corners at level 0: 1
Corners at level 1: 2 3
Corners at level 2: 4 7


Example 2:

Input:

        10
      /    \
     20     30
    / \  
   40  60

Output: 10 20 30 40 60


Your Task:  
You dont need to read input or print anything. Complete the function printCorner() which takes root node as input parameter and prints the corner nodes separated by spaces. The left corner should be printed before the right for each level starting from level 0.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(number of nodes in a level)


Constraints:
1 ≤ N ≤ 10^5

 

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Output Window