Leaderboard
Showing:
Handle Score
@Ibrahim Nash 6379
@blackshadows 6329
@mb1973 5358
@Quandray 5231
@akhayrutdinov 5111
@saiujwal13083 4510
@sanjay05 3762
@marius_valentin_dragoi 3522
@sushant_a 3459
@verma_ji 3357
@KshamaGupta 3318
Complete Leaderboard
Leftmost and rightmost nodes of binary tree
Medium Accuracy: 50.0% Submissions: 11265 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.

Leftmost and rightmost nodes of binary tree

Output Window