Level order traversal is one of common interview question and it creates foundation of various other problems.Displaying nodes in reverse order is one of the variation of level order traversal.
In order to visit tree nodes in reverse order (level order fashion bottom to top), we need to store dequeued nodes from queue into a stack and iterate stack to display nodes in reverse order.Consider following tree and its level order traversal node sequence.
Reverse level order traversal nodes sequence :- 56 78 32 98 11 43 12 27 23 18 12
Algorithm:-
1. Do check for empty tree(root is not null), enqueue root into queue.
2. Loop until queue is not empty
Dequeue node from queue and push it into stack. Check for left and right child, if exist enqueues
them into queue, right child followed by left child.
3. Repeat step 2.
3. Exit loop when queue becomes empty
Explanation:- Extra space of order O(n) is used as stack and on each dequeue operation stack is filled. Right child followed by left child enqueued into queue because we need to pop left node first from stack and stack follows LIFO property.
================Sample output==============
Reverse level order traversal(bottom to top) : 56 78 32 98 11 43 12 27 23 18 12
=========================================
In order to visit tree nodes in reverse order (level order fashion bottom to top), we need to store dequeued nodes from queue into a stack and iterate stack to display nodes in reverse order.Consider following tree and its level order traversal node sequence.
Reverse level order traversal nodes sequence :- 56 78 32 98 11 43 12 27 23 18 12
Algorithm:-
1. Do check for empty tree(root is not null), enqueue root into queue.
2. Loop until queue is not empty
Dequeue node from queue and push it into stack. Check for left and right child, if exist enqueues
them into queue, right child followed by left child.
3. Repeat step 2.
3. Exit loop when queue becomes empty
Sample code for iterative reverse level order traversal :-
public void reverseIterativelevelOrderTraversal(Node root) {
/* java.util.ArrayDeque and java.util.Stack */
Queue<Node> queue = new ArrayDeque<>();
Stack<Node> stack = new Stack<>();
if (root != null) {
queue.add(root);
while (!queue.isEmpty()) {
/* Dequeue form queue */
root = queue.remove();
stack.push(root);
Node lc = root.leftChild;
Node rc = root.rightChild;
/* Enqueue into queue first right followed by left child */
if (rc != null) {
queue.add(rc);
}
if (lc != null) {
queue.add(lc);
}
}
}
while (!stack.isEmpty()) {
System.out.print(stack.pop().data + " ");
}
}
Complete sample program for reverse level order traversal
package com.devinline.trees; |
Reverse level order traversal(bottom to top) : 56 78 32 98 11 43 12 27 23 18 12
=========================================
Mua vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ khứ hồi
chuyến bay cứu trợ mỹ về việt nam
chuyến bay từ frankfurt đến hà nội
bay từ nga về việt nam
giá thuê máy bay từ anh về việt nam
ve may bay tu phap ve viet nam
khách sạn cách ly hồ chí minh
Chuyen bay cho chuyen gia nuoc ngoai