-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathBT_Problem_02.java
More file actions
104 lines (85 loc) · 2.23 KB
/
Copy pathBT_Problem_02.java
File metadata and controls
104 lines (85 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package trees.binaryTree;
import java.util.*;
class Node {
int data;
int hd;
Node left, right;
public Node(int item) {
data = item;
hd = Integer.MAX_VALUE;
left = right = null;
}
}
// Find Reverse Level Order traversal
public class BT_Problem_02 {
Node root;
/* Given a binary tree, print its nodes in reverse level order */
@SuppressWarnings({ "unchecked", "rawtypes" })
void reverseLevelOrder(Node node) {
// Logic for O(n^2) Time Complexity approach
// int h = height(node);
// int i;
// for(i = h; i >= 1; i--)
// printGivenLevel(node, i);
Stack<Node> S = new Stack();
Queue<Node> Q = new LinkedList();
Q.add(node);
// Do something like normal level order traversal order.
// Following are the differences with normal level order traversal:
// 1. Instead of printing a node, we push the node to stack
// 2. Right subtree is visited before left subtree.
while (Q.isEmpty() == false) {
// Dequeue node and make it root
node = Q.peek();
Q.remove();
S.push(node);
// Enqueue right child
if (node.right != null)
Q.add(node.right); // Node RIGHT CHILD IS ENQUEUED BEFORE LEFT
// Enqueue left child
if (node.left != null)
Q.add(node.left);
}
// Now pop all items from stack one by one and print them
while (S.empty() == false) {
node = S.peek();
System.out.print(node.data + " ");
S.pop();
}
}
// void printGivenLevel(Node node, int level) {
// if(node == null)
// return;
// if(level == 1)
// System.out.print(node.data + " ");
// else if(level > 1) {
// printGivenLevel(node.left, level - 1);
// printGivenLevel(node.right, level - 1);
// }
// }
//
// int height(Node node) {
// if(node == null)
// return 0;
// else {
// int lheight = height(node.left);
// int rheight = height(node.right);
//
// if(lheight > rheight)
// return (lheight + 1);
// else
// return (rheight + 1);
// }
// }
//
public static void main(String[] args) {
BT_Problem_02 tree = new BT_Problem_02();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
System.out.println("Level Order traversal of binary tree is : ");
tree.reverseLevelOrder(tree.root);
}
}