Given a Perfect Binary Tree like below:
(click on image to get a clear view)
Print the level order of nodes in following specific manner:
1 2 3 4 7 5 6 8 15 9 14 10 13 11 12 16 31 17 30 18 29 19 28 20 27 21 26 22 25 23 24
i.e. print nodes in level order but nodes should be from left and right side alternatively. Here 1st and 2nd levels are trivial.
While 3rd level: 4(left), 7(right), 5(left), 6(right) are printed.
While 4th level: 8(left), 15(right), 9(left), 14(right), .. are printed.
While 5th level: 16(left), 31(right), 17(left), 30(right), .. are printed.
We strongly recommend to minimize your browser and try this yourself first.
In standard Level Order Traversal, we enqueue root into a queue 1st, then we dequeue ONE node from queue, process (print) it, enqueue its children into queue. We keep doing this until queue is empty.
We can do standard level order traversal here too but instead of printing nodes directly, we have to store nodes in current level in a temporary array or list 1st and then take nodes from alternate ends (left and right) and print nodes. Keep repeating this for all levels.
This approach takes more memory than standard traversal.
The standard level order traversal idea will slightly change here. Instead of processing ONE node at a time, we will process TWO nodes at a time. And while pushing children into queue, the enqueue order will be: 1st node’s left child, 2nd node’s right child, 1st node’s right child and 2nd node’s left child.
Specific Level Order traversal of binary tree is 1 2 3 4 7 5 6 8 15 9 14 10 13 11 12 16 31 17 30 18 29 19 28 20 27 21 26 22 25 23 24
- The above code prints specific level order from TOP to BOTTOM. How will you do specific level order traversal from BOTTOM to TOP (Amazon Interview | Set 120 – Round 1 Last Problem)
- What if tree is not perfect, but complete.
- What if tree is neither perfect, nor complete. It can be any general binary tree.
This article is contributed by Anurag Singh. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Perfect Binary Tree Specific Level Order Traversal | Set 2
- 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
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Level Order Tree Traversal
- Zig Zag Level order traversal of a tree using single queue
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Flatten binary tree in order of post-order traversal
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Print middle level of perfect binary tree without finding height
- Insertion in a Binary Tree in level order
- Level Order Predecessor of a node in Binary Tree
- Level Order Successor of a node in Binary Tree
Improved By : shrikanth13