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.
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
- 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 the 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:
10 13 13 14 15 21 21 22 22 8
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Level order traversal of Binary Tree using Morris Traversal
- Flatten Binary Tree in order of Level Order Traversal
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Insertion in n-ary tree in given order and Level order traversal
- Boundary Traversal of binary tree
- Iterative Boundary Traversal of Complete Binary tree
- Boundary Root to Leaf Path traversal of a Binary Tree
- Connect Nodes at same Level (Level Order Traversal)
- Perfect Binary Tree Specific Level Order Traversal
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- 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
- Density of Binary Tree using Level Order Traversal
- Calculate height of Binary Tree using Inorder and Level Order Traversal
- Check if the level order traversal of a Binary Tree results in a palindrome
- Deletion of a given node K in a Binary Tree using Level Order Traversal
- Specific Level Order Traversal of Binary Tree
- Level Order Binary Tree Traversal
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.