Given a Binary Tree, print the nodes level wise, each level on a new line.
Output: 1 2 3 4 5
We have discussed two solution in below articles.
Print level order traversal line by line | Set 1
Level order traversal line by line | Set 2 (Using Two Queues)
In this post, a different approach using one queue is discussed. First insert the root and a null element into the queue. This null element acts as a delimiter. Next pop from the top of the queue and add its left and right nodes to the end of the queue and then print the top of the queue. Continue this process till the queues become empty.
1 2 3 4 5 6
Time Complexity : O(n)
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
- Print level order traversal line by line | Set 1
- Level order traversal line by line | Set 2 (Using Two Queues)
- Level order traversal in spiral form | Using one stack and one queue
- Connect Nodes at same Level (Level Order Traversal)
- Print a Binary Tree in Vertical Order | Set 3 (Using Level Order Traversal)
- Flatten Binary Tree in order of Level Order Traversal
- Insertion in n-ary tree in given order and Level order traversal
- Construct BST from its given level order traversal
- Construct BST from its given level order traversal | Set-2
- Reverse Level Order Traversal
- Level Order Tree Traversal
- Level order traversal in spiral form
- Zig Zag Level order traversal of a tree using single array
- Reverse Level Order traversal in spiral form
- Check if two trees are mirror of each other using level order 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 email@example.com. 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.