Boundary Level order traversal of a Binary Tree
Given a Binary Tree, the task is to print all levels of this tree in Boundary Level order traversal.
Boundary Level order traversal: In this traversal, the first element of the level (starting boundary) is printed first, followed by last element (ending boundary). Then the process is repeated for the second and last-second element, till the complete level has been printed.
Examples:
Input:
1
/ \
12 13
/ \ / \
11 6 4 11
/ / \ / \
23 7 9 2 4
Output:
1
12 13
11 11 6 4
23 4 7 2 9
Input:
7
/ \
22 19
/ \ \
3 6 13
/ \ \ / \
1 5 8 1 4
/
23
Output:
7
22 19
3 13 6
1 4 5 1 8
23
Approach:
- In order to print level in Boundary Level order traversal, we need to first do the Level Order Traversal of the Binary tree to get the values at each level.
- Here a Queue data structure is used to store the levels of the Tree while doing the Level Order Traversal.
- Then for each level, the first element of the level (starting boundary) is printed first, followed by last element (ending boundary). Then the process is repeated for the second and last-second element, till the complete level has been printed.
Below is the implementation of the above approach:
CPP
// C++ program for printing a // Levels of Binary Tree in a // start end fashion #include <bits/stdc++.h> using namespace std; // A Tree node struct Node { int key; struct Node *left, *right; }; // Utility function to create a new node Node* newNode(int key) { Node* temp = new Node; temp->key = key; temp->left = temp->right = NULL; return (temp); } // Utility function to print level in // start end fashion void printLevelUtil(struct Node* queue[], int index, int size) { while (index < size) { cout << queue[index++]->key << " " << queue[size--]->key << " "; } if (index == size) { cout << queue[index]->key << " "; } cout << endl; } // Utility function to print level in start // end fashion in a given Binary tree void printLevel(struct Node* node, struct Node* queue[], int index, int size) { // Print root node value // as a single value in a // binary tree cout << queue[index]->key << endl; // Level order traversal of Tree while (index < size) { int curr_size = size; while (index < curr_size) { struct Node* temp = queue[index]; if (temp->left != NULL) { queue[size++] = temp->left; } if (temp->right != NULL) { queue[size++] = temp->right; } index++; } // Print level in a desire fashion printLevelUtil(queue, index, size - 1); } } // Function to find total no of nodes int findSize(struct Node* node) { if (node == NULL) return 0; return 1 + findSize(node->left) + findSize(node->right); } // Function to print level in start end // fashion in a given binary tree void printLevelInStartEndFashion( struct Node* node) { int t_size = findSize(node); struct Node* queue[t_size]; queue[0] = node; printLevel(node, queue, 0, 1); } // Driver Code int main() { /* 10 / \ 13 13 / \ 14 15 / \ / \ 21 22 22 21 / 8 */ // Create Binary Tree as shown Node* root = newNode(10); root->left = newNode(13); root->right = newNode(13); root->right->left = newNode(14); root->right->right = newNode(15); root->right->left->left = newNode(21); root->right->left->right = newNode(22); root->right->right->left = newNode(22); root->right->right->right = newNode(21); root->right->right->right->left = newNode(8); // Print Levels In Start End Fashion printLevelInStartEndFashion(root); return 0; } |
Java
// Java program for printing a // Levels of Binary Tree in a // start end fashion class GFG{ // A Tree node static class Node { int key; Node left, right; }; // Utility function to create a new node static Node newNode(int key) { Node temp = new Node(); temp.key = key; temp.left = temp.right = null; return (temp); } // Utility function to print level in // start end fashion static void printLevelUtil(Node queue[], int index, int size) { while (index < size) { System.out.print(queue[index++].key+ " " + queue[size--].key+ " "); } if (index == size) { System.out.print(queue[index].key+ " "); } System.out.println(); } // Utility function to print level in start // end fashion in a given Binary tree static void printLevel(Node node, Node queue[], int index, int size) { // Print root node value // as a single value in a // binary tree System.out.print(queue[index].key +"\n"); // Level order traversal of Tree while (index < size) { int curr_size = size; while (index < curr_size) { Node temp = queue[index]; if (temp.left != null) { queue[size++] = temp.left; } if (temp.right != null) { queue[size++] = temp.right; } index++; } // Print level in a desire fashion printLevelUtil(queue, index, size - 1); } } // Function to find total no of nodes static int findSize(Node node) { if (node == null) return 0; return 1 + findSize(node.left) + findSize(node.right); } // Function to print level in start end // fashion in a given binary tree static void printLevelInStartEndFashion( Node node) { int t_size = findSize(node); Node []queue = new Node[t_size]; queue[0] = node; printLevel(node, queue, 0, 1); } // Driver Code public static void main(String[] args) { /* 10 / \ 13 13 / \ 14 15 / \ / \ 21 22 22 21 / 8 */ // Create Binary Tree as shown Node root = newNode(10); root.left = newNode(13); root.right = newNode(13); root.right.left = newNode(14); root.right.right = newNode(15); root.right.left.left = newNode(21); root.right.left.right = newNode(22); root.right.right.left = newNode(22); root.right.right.right = newNode(21); root.right.right.right.left = newNode(8); // Print Levels In Start End Fashion printLevelInStartEndFashion(root); } } // This code is contributed by Princi Singh |
C#
// C# program for printing a // Levels of Binary Tree in a // start end fashion using System; class GFG{ // A Tree node class Node { public int key; public Node left, right; }; // Utility function to create a new node static Node newNode(int key) { Node temp = new Node(); temp.key = key; temp.left = temp.right = null; return (temp); } // Utility function to print level in // start end fashion static void printLevelUtil(Node []queue, int index, int size) { while (index < size) { Console.Write(queue[index++].key+ " " + queue[size--].key+ " "); } if (index == size) { Console.Write(queue[index].key+ " "); } Console.WriteLine(); } // Utility function to print level in start // end fashion in a given Binary tree static void printLevel(Node node, Node []queue, int index, int size) { // Print root node value // as a single value in a // binary tree Console.Write(queue[index].key +"\n"); // Level order traversal of Tree while (index < size) { int curr_size = size; while (index < curr_size) { Node temp = queue[index]; if (temp.left != null) { queue[size++] = temp.left; } if (temp.right != null) { queue[size++] = temp.right; } index++; } // Print level in a desire fashion printLevelUtil(queue, index, size - 1); } } // Function to find total no of nodes static int findSize(Node node) { if (node == null) return 0; return 1 + findSize(node.left) + findSize(node.right); } // Function to print level in start end // fashion in a given binary tree static void printLevelInStartEndFashion( Node node) { int t_size = findSize(node); Node []queue = new Node[t_size]; queue[0] = node; printLevel(node, queue, 0, 1); } // Driver Code public static void Main(String[] args) { /* 10 / \ 13 13 / \ 14 15 / \ / \ 21 22 22 21 / 8 */ // Create Binary Tree as shown Node root = newNode(10); root.left = newNode(13); root.right = newNode(13); root.right.left = newNode(14); root.right.right = newNode(15); root.right.left.left = newNode(21); root.right.left.right = newNode(22); root.right.right.left = newNode(22); root.right.right.right = newNode(21); root.right.right.right.left = newNode(8); // Print Levels In Start End Fashion printLevelInStartEndFashion(root); } } // This code is contributed by PrinciRaj1992 |
10 13 13 14 15 21 21 22 22 8
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- 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
- Density of Binary Tree using Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Check if the level order traversal of a Binary Tree results in a palindrome
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Build Binary Tree from BST such that it's level order traversal prints sorted data
- Boundary Traversal of binary tree
- Iterative Boundary Traversal of Complete Binary tree
- Insertion in n-ary tree in given order and Level order traversal
- Boundary Root to Leaf Path traversal of a Binary Tree
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

