Print leftmost and rightmost nodes of a Binary Tree
Last Updated :
11 Jan, 2023
Given a Binary Tree, Print the corner nodes at each level. The node at the leftmost and the node at the rightmost.
For example, the output for the following is 15, 10, 20, 8, 25.
A Simple Solution is to do two traversals using the approaches discussed for printing left view and right view.
Can we print all corner nodes using one traversal?
The idea is to use Level Order Traversal. Every time we store the size of the queue in a variable n, which is the number of nodes at that level. For every level, we check whether the current node is the first (i.e node at index 0) and the node at the last index (i.e node at index n-1) If it is either of them, we print the value of that node.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int key;
struct Node* left, *right;
};
struct Node* newNode( int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
void printCorner(Node *root)
{
if (root == NULL)
return ;
queue<Node*> q;
q.push(root);
while (!q.empty())
{
int n = q.size();
for ( int i =0;i<n;i++)
{
Node *temp = q.front();
q.pop();
if (i==0 || i==n-1)
cout<<temp->key<< " " ;
if (temp->left)
q.push(temp->left);
if (temp->right)
q.push(temp->right);
}
}
}
int main ()
{
Node *root = newNode(15);
root->left = newNode(10);
root->right = newNode(20);
root->left->left = newNode(8);
root->left->right = newNode(12);
root->right->left = newNode(16);
root->right->right = newNode(25);
printCorner(root);
return 0;
}
|
Java
import java.util.*;
class Node
{
int key;
Node left, right;
public Node( int key)
{
this .key = key;
left = right = null ;
}
}
class BinaryTree
{
Node root;
void printCorner(Node root)
{
Queue<Node> q = new LinkedList<Node>();
q.add(root);
while (!q.isEmpty())
{
int n = q.size();
for ( int i = 0 ; i < n ; i++){
Node temp = q.peek();
q.poll();
if (i== 0 || i==n- 1 )
System.out.print(temp.key + " " );
if (temp.left != null )
q.add(temp.left);
if (temp.right != null )
q.add(temp.right);
}
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 15 );
tree.root.left = new Node( 10 );
tree.root.right = new Node( 20 );
tree.root.left.left = new Node( 8 );
tree.root.left.right = new Node( 12 );
tree.root.right.left = new Node( 16 );
tree.root.right.right = new Node( 25 );
tree.printCorner(tree.root);
}
}
|
Python3
from collections import deque
class Node:
def __init__( self , key):
self .key = key
self .left = None
self .right = None
def printCorner(root: Node):
if root = = None :
return
q = deque()
q.append(root)
while q:
n = len (q)
for i in range (n):
temp = q[ 0 ]
q.popleft()
if i = = 0 or i = = n - 1 :
print (temp.key, end = " " )
if temp.left:
q.append(temp.left)
if temp.right:
q.append(temp.right)
if __name__ = = "__main__" :
root = Node( 15 )
root.left = Node( 10 )
root.right = Node( 20 )
root.left.left = Node( 8 )
root.left.right = Node( 12 )
root.right.left = Node( 16 )
root.right.right = Node( 25 )
printCorner(root)
|
C#
using System;
using System.Collections.Generic;
public class Node
{
public int key;
public Node left, right;
public Node( int key)
{
this .key = key;
left = right = null ;
}
}
public class BinaryTree
{
Node root;
void printCorner(Node root)
{
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count != 0)
{
int n = q.Count;
for ( int i = 0 ; i < n ; i++){
Node temp = q.Peek();
q.Dequeue();
if (i==0||i==n-1)
Console.Write(temp.key + " " );
if (temp.left != null )
q.Enqueue(temp.left);
if (temp.right != null )
q.Enqueue(temp.right);
}
}
}
public static void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(15);
tree.root.left = new Node(10);
tree.root.right = new Node(20);
tree.root.left.left = new Node(8);
tree.root.left.right = new Node(12);
tree.root.right.left = new Node(16);
tree.root.right.right = new Node(25);
tree.printCorner(tree.root);
}
}
|
Javascript
class Node{
constructor(val){
this .key = val;
this .left = null ;
this .right = null ;
}
}
function printCorner(root){
if (root == null ) return ;
var q = [];
q.push(root);
while (q.length != 0){
var n = q.length;
for (let i = 0; i < n; i++){
let temp = q.shift();
if (i == 0 || i == n-1){
console.log(temp.key);
}
if (temp.left != null ) q.push(temp.left);
if (temp.right != null ) q.push(temp.right);
}
}
}
var root = new Node(15);
root.left = new Node(10);
root.right = new Node(20);
root.left.left = new Node(8);
root.left.right = new Node(12);
root.right.left = new Node(16);
root.right.right = new Node(25);
printCorner(root);
|
Time Complexity: O(n) where n is the number of nodes in the Binary Tree.
Auxiliary Space: O(n).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...