Print extreme nodes of each level of Binary Tree in alternate order
Given a binary tree, print nodes of extreme corners of each level but in alternate order.
Example:

For above tree, the output can be
1 2 7 8 31
– print rightmost node of 1st level
– print leftmost node of 2nd level
– print rightmost node of 3rd level
– print leftmost node of 4th level
– print rightmost node of 5th level
OR
1 3 4 15 16
– print leftmost node of 1st level
– print rightmost node of 2nd level
– print leftmost node of 3rd level
– print rightmost node of 4th level
– print leftmost node of 5th level
The idea is to traverse tree level by level. For each level, we count number of nodes in it and print its leftmost or the rightmost node based on value of a Boolean flag. We dequeue all nodes of current level and enqueue all nodes of next level and invert value of Boolean flag when switching levels.
Below is the implementation of above idea –
C++
/* C++ program to print nodes of extreme corners of each level in alternate order */#include <bits/stdc++.h> using namespace std; /* A binary tree node has data, pointer to left child and a pointer to right child */struct Node { int data; Node *left, *right; }; /* Helper function that allocates a new node with the given data and NULL left and right pointers. */Node* newNode(int data) { Node* node = new Node; node->data = data; node->right = node->left = NULL; return node; } /* Function to print nodes of extreme corners of each level in alternate order */void printExtremeNodes(Node* root) { if (root == NULL) return; // Create a queue and enqueue left and right // children of root queue<Node*> q; q.push(root); // flag to indicate whether leftmost node or // the rightmost node has to be printed bool flag = false; while (!q.empty()) { // nodeCount indicates number of nodes // at current level. int nodeCount = q.size(); int n = nodeCount; // Dequeue all nodes of current level // and Enqueue all nodes of next level while (n--) { Node* curr = q.front(); // Enqueue left child if (curr->left) q.push(curr->left); // Enqueue right child if (curr->right) q.push(curr->right); // Dequeue node q.pop(); // if flag is true, print leftmost node if (flag && n == nodeCount - 1) cout << curr->data << " "; // if flag is false, print rightmost node if (!flag && n == 0) cout << curr->data << " "; } // invert flag for next level flag = !flag; } } /* Driver program to test above functions */int main() { // Binary Tree of Height 4 Node* root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->right = newNode(7); root->left->left->left = newNode(8); root->left->left->right = newNode(9); root->left->right->left = newNode(10); root->left->right->right = newNode(11); root->right->right->left = newNode(14); root->right->right->right = newNode(15); root->left->left->left->left = newNode(16); root->left->left->left->right = newNode(17); root->right->right->right->right = newNode(31); printExtremeNodes(root); return 0; } |
Java
// Java program to print nodes of extreme corners //of each level in alternate order import java.util.*; class GFG { // A binary tree node has data, pointer to left child //and a pointer to right child / static class Node { int data; Node left, right; }; // Helper function that allocates a new node with the //given data and null left and right pointers. / static Node newNode(int data) { Node node = new Node(); node.data = data; node.right = node.left = null; return node; } // Function to print nodes of extreme corners //of each level in alternate order static void printExtremeNodes(Node root) { if (root == null) return; // Create a queue and enqueue left and right // children of root Queue<Node> q = new LinkedList<Node>(); q.add(root); // flag to indicate whether leftmost node or // the rightmost node has to be printed boolean flag = false; while (q.size()>0) { // nodeCount indicates number of nodes // at current level. int nodeCount = q.size(); int n = nodeCount; // Dequeue all nodes of current level // and Enqueue all nodes of next level while (n-->0) { Node curr = q.peek(); // Enqueue left child if (curr.left!=null) q.add(curr.left); // Enqueue right child if (curr.right!=null) q.add(curr.right); // Dequeue node q.remove(); // if flag is true, print leftmost node if (flag && n == nodeCount - 1) System.out.print( curr.data + " "); // if flag is false, print rightmost node if (!flag && n == 0) System.out.print( curr.data + " "); } // invert flag for next level flag = !flag; } } // Driver code public static void main(String args[]) { // Binary Tree of Height 4 Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); root.right.right = newNode(7); root.left.left.left = newNode(8); root.left.left.right = newNode(9); root.left.right.left = newNode(10); root.left.right.right = newNode(11); root.right.right.left = newNode(14); root.right.right.right = newNode(15); root.left.left.left.left = newNode(16); root.left.left.left.right = newNode(17); root.right.right.right.right = newNode(31); printExtremeNodes(root); } } // This code is contributed by Arnab Kundu |
C#
// C# program to print nodes of extreme corners //of each level in alternate order using System; using System.Collections.Generic; class GFG { // A binary tree node has data, pointer to left child //and a pointer to right child / public class Node { public int data; public Node left, right; }; // Helper function that allocates a new node with the //given data and null left and right pointers. / static Node newNode(int data) { Node node = new Node(); node.data = data; node.right = node.left = null; return node; } // Function to print nodes of extreme corners //of each level in alternate order static void printExtremeNodes(Node root) { if (root == null) return; // Create a queue and enqueue left and right // children of root Queue<Node> q = new Queue<Node>(); q.Enqueue(root); // flag to indicate whether leftmost node or // the rightmost node has to be printed Boolean flag = false; while (q.Count > 0) { // nodeCount indicates number of nodes // at current level. int nodeCount = q.Count; int n = nodeCount; // Dequeue all nodes of current level // and Enqueue all nodes of next level while (n-->0) { Node curr = q.Peek(); // Enqueue left child if (curr.left != null) q.Enqueue(curr.left); // Enqueue right child if (curr.right != null) q.Enqueue(curr.right); // Dequeue node q.Dequeue(); // if flag is true, print leftmost node if (flag && n == nodeCount - 1) Console.Write( curr.data + " "); // if flag is false, print rightmost node if (!flag && n == 0) Console.Write( curr.data + " "); } // invert flag for next level flag = !flag; } } // Driver code public static void Main(String []args) { // Binary Tree of Height 4 Node root = newNode(1); root.left = newNode(2); root.right = newNode(3); root.left.left = newNode(4); root.left.right = newNode(5); root.right.right = newNode(7); root.left.left.left = newNode(8); root.left.left.right = newNode(9); root.left.right.left = newNode(10); root.left.right.right = newNode(11); root.right.right.left = newNode(14); root.right.right.right = newNode(15); root.left.left.left.left = newNode(16); root.left.left.left.right = newNode(17); root.right.right.right.right = newNode(31); printExtremeNodes(root); } } // This code is contributed by Rajput-Ji |
Output:
1 2 7 8 31
Time complexity of above solution is O(n) where n is total number of nodes in given binary tree.
Exercise – Print nodes of extreme corners of each level from bottom to top in alternate order.
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Print nodes between two given level numbers of a binary tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Difference between sums of odd level and even level nodes of a Binary Tree
- Flatten Binary Tree in order of Level Order Traversal
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Insertion in a Binary Tree in level order
- Swap Nodes in Binary tree of every k'th level
- Density of Binary Tree using Level Order Traversal
- Level Order Successor of a node in Binary Tree



