# Print leftmost and rightmost nodes of a Binary Tree

Given a Binary Tree, Print the corner nodes at each level. The node at the leftmost and the node at the rightmost.

For example, output for following is 15, 10, 20, 8, 25.

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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 three conditions, whether there is one node or more than one node, in case there is only one node we print it once and in case we have more than 1 nodes, we print the first (i.e node at index 0) and the node at last index (i.e node at index n-1).

## C++

 `// C/C++ program to print corner node at each level ` `// of binary tree ` `#include ` `using` `namespace` `std; ` ` `  `/* A binary tree node has key, pointer to left ` `   ``child and a pointer to right child */` `struct` `Node ` `{ ` `    ``int` `key; ` `    ``struct` `Node* left, *right; ` `}; ` ` `  `/* To create a newNode of tree and return pointer */` `struct` `Node* newNode(``int` `key) ` `{ ` `    ``Node* temp = ``new` `Node; ` `    ``temp->key = key; ` `    ``temp->left = temp->right = NULL; ` `    ``return` `(temp); ` `} ` ` `  `/* Function to print corner node at each level */` `void` `printCorner(Node *root) ` `{ ` `    ``//If the root is null then simply return ` `    ``if``(root == NULL) ` `        ``return``; ` `    ``//Do level order traversal using queue ` `    ``queue q; ` `    ``q.push(root); ` `     `  `    ``//The vector would store the ans ` `    ``//Note that you could simply print the corner values at each step ` `    ``vector<``int``> ans; ` `     `  `    ``while``(!q.empty()) ` `    ``{ ` `        ``//n denotes the size of the current queue ` `        ``int` `n = q.size(); ` `         `  `        ``for``(``int` `i =0;ikey); ` `           ``// rightmost corner value ` `            ``else` `if``(i==n-1) ` `                ``ans.push_back(temp->key); ` `            `  `                 `  `            ``//push the left and right children of the temp node ` `            ``if``(temp->left) ` `                ``q.push(temp->left); ` `            ``if``(temp->right) ` `                ``q.push(temp->right); ` `        ``} ` `    ``} ` `    ``//loop through the variable and print the answers ` `    ``for``(``auto` `i : ans) ` `        ``cout << i << ``" "``; ` `} ` `// Driver program to test above function ` `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

 `// Java program to print corner node at each level in a binary tree ` ` `  `import` `java.util.*; ` ` `  `/* A binary tree node has key, pointer to left ` `   ``child and a pointer to right child */` `class` `Node  ` `{ ` `    ``int` `key; ` `    ``Node left, right; ` ` `  `    ``public` `Node(``int` `key)  ` `    ``{ ` `        ``this``.key = key; ` `        ``left = right = ``null``; ` `    ``} ` `} ` ` `  `class` `BinaryTree  ` `{ ` `    ``Node root; ` ` `  `    ``/* Function to print corner node at each level */` `    ``void` `printCorner(Node root) ` `    ``{ ` `        ``//  star node is for keeping track of levels ` `        ``Queue q = ``new` `LinkedList(); ` ` `  `        ``// pushing root node and star node ` `        ``q.add(root); ` `        ``q.add(``null``); ` ` `  `        ``// if isFirst = true then left most node of that level ` `        ``// will be printed ` `        ``boolean` `isFirst = ``false``; ` ` `  `        ``// if isOne = true then that level has only one node ` `        ``boolean` `isOne = ``false``; ` ` `  `        ``// last will store right most node of that level ` `        ``int` `last = ``0``; ` ` `  `        ``// Do level order traversal of Binary Tree ` `        ``while` `(!q.isEmpty())  ` `        ``{ ` `            ``// dequeue the front node from the queue ` `            ``Node temp = q.peek(); ` `            ``q.poll(); ` ` `  `            ``// if isFirst is true, then temp is leftmost node ` `            ``if` `(isFirst)  ` `            ``{ ` `                ``System.out.print(temp.key + ``"  "``); ` ` `  `                ``if` `(temp.left != ``null``) ` `                    ``q.add(temp.left); ` `                ``if` `(temp.right != ``null``) ` `                    ``q.add(temp.right); ` `                 `  `                ``// make isFirst as false and one = 1 ` `                ``isFirst = ``false``; ` `                ``isOne = ``true``; ` `            ``}  ` `             `  `            ``// Else if temp is a separator between two levels ` `            ``else` `if` `(temp == ``null``)  ` `            ``{ ` `                ``// Insert new separator if there are items in queue ` `                ``if` `(q.size() >= ``1``)  ` `                    ``q.add(``null``); ` `                 `  `                ``// making isFirst as true because next node will be ` `                ``// leftmost node of that level ` `                ``isFirst = ``true``; ` ` `  `                ``// printing last node, only if that level ` `                ``// doesn't contain single node otherwise ` `                ``// that single node will be printed twice               ` `                ``if` `(!isOne) ` `                    ``System.out.print(last + ``"  "``);     ` `            ``}  ` `            ``else` `            ``{ ` `                ``// Store current key as last ` `                ``last = temp.key; ` ` `  `                ``// Here we are making isOne = false to signify ` `                ``// that level has more than one node ` `                ``isOne = ``false``; ` `                ``if` `(temp.left != ``null``) ` `                    ``q.add(temp.left); ` `                ``if` `(temp.right != ``null``) ` `                    ``q.add(temp.right);                ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Driver program to test above functions ` `    ``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); ` `    ``} ` `} ` ` `  `// This code has been contributed by Sanay Dev `

## C#

 `// C# program to print corner node  ` `// at each level in a binary tree ` `using` `System; ` `using` `System.Collections.Generic; ` ` `  `/* A binary tree node has key, pointer to left ` `child and a pointer to right child */` `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; ` ` `  `    ``/* Function to print corner node at each level */` `    ``void` `printCorner(Node root) ` `    ``{ ` `        ``// star node is for keeping track of levels ` `        ``Queue q = ``new` `Queue(); ` ` `  `        ``// pushing root node and star node ` `        ``q.Enqueue(root); ` `        ``q.Enqueue(``null``); ` ` `  `        ``// if isFirst = true then left most node  ` `        ``// of that level will be printed ` `        ``Boolean isFirst = ``false``; ` ` `  `        ``// if isOne = true then that level has only one node ` `        ``Boolean isOne = ``false``; ` ` `  `        ``// last will store right most node of that level ` `        ``int` `last = 0; ` ` `  `        ``// Do level order traversal of Binary Tree ` `        ``while` `(q.Count != 0)  ` `        ``{ ` `            ``// dequeue the front node from the queue ` `            ``Node temp = q.Peek(); ` `            ``q.Dequeue(); ` ` `  `            ``// if isFirst is true, then temp is leftmost node ` `            ``if` `(isFirst)  ` `            ``{ ` `                ``Console.Write(temp.key + ``" "``); ` ` `  `                ``if` `(temp.left != ``null``) ` `                    ``q.Enqueue(temp.left); ` `                ``if` `(temp.right != ``null``) ` `                    ``q.Enqueue(temp.right); ` `                 `  `                ``// make isFirst as false and one = 1 ` `                ``isFirst = ``false``; ` `                ``isOne = ``true``; ` `            ``}  ` `             `  `            ``// Else if temp is a separator between two levels ` `            ``else` `if` `(temp == ``null``)  ` `            ``{ ` `                ``// Insert new separator if there are items in queue ` `                ``if` `(q.Count >= 1)  ` `                    ``q.Enqueue(``null``); ` `                 `  `                ``// making isFirst as true because next node will be ` `                ``// leftmost node of that level ` `                ``isFirst = ``true``; ` ` `  `                ``// printing last node, only if that level ` `                ``// doesn't contain single node otherwise ` `                ``// that single node will be printed twice              ` `                ``if` `(!isOne) ` `                    ``Console.Write(last + ``" "``);  ` `            ``}  ` `            ``else` `            ``{ ` `                ``// Store current key as last ` `                ``last = temp.key; ` ` `  `                ``// Here we are making isOne = false to signify ` `                ``// that level has more than one node ` `                ``isOne = ``false``; ` `                ``if` `(temp.left != ``null``) ` `                    ``q.Enqueue(temp.left); ` `                ``if` `(temp.right != ``null``) ` `                    ``q.Enqueue(temp.right);              ` `            ``} ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``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); ` `    ``} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output :

`15  10  20  8  25  `

Time Complexity : O(n) where n is number of nodes in Binary Tree.

My Personal Notes arrow_drop_up

Improved By : simplex, Rajput-Ji

Article Tags :
Practice Tags :

4

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.