Last Updated: 17 Aug, 2020

Leftmost & Rightmost Nodes of Binary Tree

Easy
Asked in companies
AmazonCultfitWizork

Problem statement

Given a Binary Tree of 'N' number of total nodes, return the sequence of values of the leftmost and rightmost node at each level.

For example:
For the given binary tree: 

alt-text

Output: 1 2 3 4 6 7 10

Explanation: The leftmost and rightmost node respectively of each level are
Level 0: 1(only one node is present at 0th level)
Level 1: 2 3
Level 2: 4 6
Level 3: 7 10
Input Format:
The only line of input contains tree elements in the level order form. The input consists of values of nodes separated by a single space in a single line. In case a node is null, we take -1 in its place.

For example, the input for the tree depicted in the below image would be :

alt text

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Explanation :

Level 1 :
The root node of the tree is 1

Level 2 :
Left child of 1 = 2
Right child of 1 = 3

Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6

Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)

Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.

The input ends when all nodes at the last level are null (-1).
Note :
The above format was just to provide clarity on how the input is formed for a given tree. 
The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output Format:
For each test case, print a single line containing space-separated integers denoting a sequence of integers written progressively from top to bottom of the tree such that for each level, left most data is followed by the rightmost one.

The output of each test case will be printed in a separate line.
Note:
You do not need to print anything; it has already been taken care of. Just implement the given function.
Constraints:
0 <= N <= 10 ^ 5

Time Limit: 1 sec.

Approaches

01 Approach

Using level-wise order traversal, we will be maintaining the queue to store the pending nodes. Starting from the 0th level i.e the root node we will add root node into the queue. Now for each level stored into the queue(initially we have 0th level). We will poll nodes one by one from the queue and add corresponding children into the queue. While popping nodes we will also print the first(leftmost node) and last(rightmost node) nodes of that level. This way we will be having another level pending in our queue to traverse. This process goes on until the last level is traversed(see below algorithm for the approach).

 

Algorithm:

  1. Initialize an empty list to store the leftmost and rightmost nodes.
  2. If the root is null, return the list.
  3. Initialize the queue storing the pending nodes and add the root in this. (marking 0th level pending).
  4. For each of the pending nodes in the queue(denoting the current level) i.e i=1 to size.
    1. Initialize current node with the queue’s front node i.e current node = queue.poll().
    2. If i == 1 or i == size, add the current node to the list. This will be denoting the leftmost or rightmost node respectively for the current level.
    3. Add the left child(if not null) of the current node to the queue.
    4. Add the right child(if not null) of the current node to the queue.
  5. Repeat step 4
  6. Return list.