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)
- Level order traversal line by line | Set 2 (Using Two Queues)
- Print level order traversal line by line | Set 1
- Zig Zag Level order traversal of a tree using single queue
- 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
- Reverse Level Order Traversal
- Construct BST from its given level order traversal | Set-2
- Level Order Tree Traversal
- Construct BST from its given level order traversal
- Level order traversal in spiral form
- Density of Binary Tree using Level Order Traversal
- Reverse Level Order traversal in spiral form
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.