-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
290 lines (240 loc) · 8.46 KB
/
Copy pathBinaryTree.java
File metadata and controls
290 lines (240 loc) · 8.46 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package binaryTree;
import template.Node;
import java.util.*;
public class BinaryTree {
/*
Tree:
1
/ \
2 3
/ \ / \
4 5 6 7
* Inorder (LDR): 4,2,5,1,6,3,7
* Preorder (DLR): 1,2,4,5,3,6,7
* PostOrder (LRD): 4,5,2,6,7,3,1
*/
static void inorder(Node root) {
if (root == null) return;
inorder(root.getLeft());
System.out.print(root.getData() + " --> ");
inorder(root.getRight());
}
static void preOrder(Node root) {
if (root == null) return;
System.out.print(root.getData() + " --> ");
preOrder(root.getLeft());
preOrder(root.getRight());
}
static void postOrder(Node root) {
if (root == null) return;
postOrder(root.getLeft());
postOrder(root.getRight());
System.out.print(root.getData() + " --> ");
}
static int minimumInATree(Node root) {
if (root == null) return Integer.MAX_VALUE;
int left = minimumInATree(root.getLeft());
int right = minimumInATree(root.getRight());
return Math.min(Math.min(left, right), root.getData());
}
static int heightOfABinaryTree(Node root) {
if (root == null) return 0;
int hl = heightOfABinaryTree(root.getLeft());
int hr = heightOfABinaryTree(root.getRight());
return Math.max(hl, hr) + 1;
}
private static Node xParent, yParent;
private static int xLevel, yLevel;
static void areCousinsHelper(Node node, Node parent, int x, int y, int level) {
if (node == null) return;
if (node.getData() == x) {
xParent = parent;
xLevel = level;
}
if (node.getData() == y) {
yParent = parent;
yLevel = level;
}
areCousinsHelper(node.getLeft(), node, x, y, level + 1);
areCousinsHelper(node.getRight(), node, x, y, level + 1);
}
static boolean checkAreCousins(Node root, int x, int y) {
/*
* 1
* / \
* 2 3 // 2 and 3 are not cousins, although they are on same level but their parent is same i.e 1
* \ \
* 4 5 // 4 and 5 are cousins because they are on same level and have different parent
*
* */
areCousinsHelper(root, null, x, y, 0);
boolean onSameLevel = xLevel == yLevel;
boolean differentParent = xParent != yParent;
return onSameLevel && differentParent;
}
static void printAllNodesAtALevelKRecursive(Node root, int level, ArrayList<Integer> ans) {
if (root == null || level < 0) return;
if (level == 0) {
ans.add(root.getData());
}
printAllNodesAtALevelKRecursive(root.getLeft(), level - 1, ans);
printAllNodesAtALevelKRecursive(root.getRight(), level - 1, ans);
}
static class Pair {
Node node;
int level;
public Pair(Node node, int level) {
this.node = node;
this.level = level;
}
}
static List<Integer> printAllNodesAtALevelKIterative(Node root, int level) {
ArrayList<Integer> ans = new ArrayList<>();
ArrayDeque<Pair> stack = new ArrayDeque<>();
Pair first = new Pair(root, level);
stack.push(first);
while (!stack.isEmpty()) {
Pair curr = stack.poll();
if (curr.node == null) continue;
if (curr.level == 0) {
ans.add(curr.node.getData());
continue;
}
stack.push(new Pair(curr.node.getRight(), curr.level - 1));
stack.push(new Pair(curr.node.getLeft(), curr.level - 1));
}
return ans;
}
static ArrayList<Integer> inOrder(Node root) {
ArrayList<Integer> res = new ArrayList<>();
Stack<Node> stack = new Stack<Node>();
stack.push(root);
while (!stack.isEmpty()) {
Node temp = stack.pop();
if (temp.getRight() != null) {
stack.push(temp.getRight());
}
if (temp.getLeft() != null) {
stack.push(temp.getLeft());
}
}
System.out.println(stack);
return res;
}
static void printRightViewOfABinaryTree(Node root, int level, ArrayList<Integer> res) {
if (root == null) return;
if (level >= res.size()) {
res.add(root.getData());
}
printRightViewOfABinaryTree(root.getRight(), level + 1, res);
printRightViewOfABinaryTree(root.getLeft(), level + 1, res);
}
static void printLevelOrderTraversal(Node root) {
ArrayList<Integer> res = new ArrayList<>();
Queue<Node> queue = new LinkedList<Node>();
queue.offer(root);
while (!queue.isEmpty()) {
Node curr = queue.poll();
res.add(curr.getData());
if (curr.getLeft() != null) {
queue.add(curr.getLeft());
}
if (curr.getRight() != null) {
queue.add(curr.getRight());
}
}
System.out.println(res);
}
static class Pair1 {
//horizontal distance
int hd;
Node node;
public Pair1(int hd, Node node) {
this.hd = hd;
this.node = node;
}
}
static ArrayList<ArrayList<Integer>> verticalOrderTraversal(Node root) {
Queue<Pair1> q = new LinkedList<>();
TreeMap<Integer, ArrayList<Integer>> map = new TreeMap<>();
q.add(new Pair1(0, root));
while (!q.isEmpty()) {
Pair1 curr = q.poll();
if (!map.containsKey(curr.hd)) {
map.putIfAbsent(curr.hd, new ArrayList<>());
}
map.get(curr.hd).add(curr.node.getData());
if (curr.node.getLeft() != null) {
q.add(new Pair1(curr.hd - 1, curr.node.getLeft()));
}
if (curr.node.getRight() != null) {
q.add(new Pair1(curr.hd + 1, curr.node.getRight()));
}
}
return new ArrayList<>(map.values());
}
static ArrayList<Integer> printTopViewOfABinaryTree(Node root) {
Queue<Pair1> q = new LinkedList<>();
TreeMap<Integer, Integer> map = new TreeMap<>();
q.add(new Pair1(0, root));
while (!q.isEmpty()) {
Pair1 curr = q.poll();
if (!map.containsKey(curr.hd)) {
map.put(curr.hd, curr.node.getData());
}
if (curr.node.getLeft() != null) {
q.add(new Pair1(curr.hd - 1, curr.node.getLeft()));
}
if (curr.node.getRight() != null) {
q.add(new Pair1(curr.hd + 1, curr.node.getRight()));
}
}
return new ArrayList<>(map.values());
}
static int DIAMETER = 0;
static int diameterOfABinaryTreeHelper(Node root) {
if (root == null) return 0;
int leftHeight = diameterOfABinaryTreeHelper(root.getLeft());
int rightHeight = diameterOfABinaryTreeHelper(root.getRight());
DIAMETER = Math.max(DIAMETER, leftHeight + rightHeight);
return Math.max(leftHeight, rightHeight) + 1;
}
public static void main(String[] args) {
Node root = new Node(1);
Node n1 = new Node(2);
Node n2 = new Node(3);
Node n3 = new Node(4);
Node n4 = new Node(5);
Node n5 = new Node(6);
Node n6 = new Node(7);
root.setLeft(n1);
root.setRight(n2);
n1.setLeft(n3);
n1.setRight(n4);
n2.setLeft(n5);
n2.setRight(n6);
/*
1
2 3
4 5 6 7
*/
// inorder(root);
// preOrder(root);
// postOrder(root);
// System.out.println(minimumInATree(root));
// System.out.println(heightOfABinaryTree(root));
// System.out.println(checkAreCousins(root, n3.getData(), n4.getData()));]
// ArrayList<Integer> ans = new ArrayList<>();
// printAllNodesAtALevelK(root, 1, ans);
// System.out.println(ans);
// System.out.println(printAllNodesAtALevelKIterative(root, 2));
// System.out.println(inOrder(root));a
// ArrayList<Integer> res = new ArrayList<>();
// printRightViewOfABinaryTree(root, 0, res);
// System.out.println(res);
// printLevelOrderTraversal(root);
// System.out.println(printTopViewOfABinaryTree(root));
diameterOfABinaryTreeHelper(root);
System.out.println(DIAMETER);
}
}