-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlattenBinaryTreeToLinkedList.java
More file actions
363 lines (245 loc) · 9.45 KB
/
FlattenBinaryTreeToLinkedList.java
File metadata and controls
363 lines (245 loc) · 9.45 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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package Algorithms.BinaryTrees;
import java.util.Stack;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 29 Jan 2025
* @link 114. Flatten Binary Tree to Linked List <a href="https://leetcode.com/problems/flatten-binary-tree-to-linked-list/">LeetCode Link</a>
* @topics Tree, Binary Tree, LinkedList, DFS, Stack
* @companies Amazon(2), Google(4), Meta(2), Bloomberg(2), Microsoft(11), Oracle(3), Yahoo(2)
*/
public class FlattenBinaryTreeToLinkedList {
public static class TreeNode {int val;TreeNode left, right;TreeNode() {}TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}}
public static void main(String[] args) {
TreeNode root;
/*
1
/ \
2 5
/ \ \
3 4 6
=> 1-2-3-4-5-6
THOUGHTS:
--------
1. preserve root.right in one temp var
2. now root.right = root.left
3. at root.right leaf node add that temp var value
4. repeat this until we no longer have anything to add to temp
*/
System.out.println("\nFlatten Using Iteration:");
root = buildTree();
flattenUsingIteration(root);
printTree(root);
System.out.println("\nFlatten Using Recursion:");
root = buildTree();
flattenUsingRecursion(root);
printTree(root);
System.out.println("\nFlatten Using Dfs:");
root = buildTree();
flattenUsingDfs(root);
printTree(root);
System.out.println("\nFlatten Using Stack:");
root = buildTree();
flattenUsingStack(root);
printTree(root);
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(1)
1 ---> current
/ \
2 5
/ \ \
3 4 6
will become
1
\
2 ---> current
/ \
3 4
\
5
\
6
will become
1
\
2
\
3 ---> current
\
4
\
5
\
6
similarly, we traverse all right nodes
*/
public static void flattenUsingIteration(TreeNode root) {
while (root != null) {
if (root.left != null) {
TreeNode leftLeaf = root.left;
while (leftLeaf.right != null) leftLeaf = leftLeaf.right;
leftLeaf.right = root.right; // Morris traversal main step
root.right = root.left;
root.left = null;
}
root = root.right; // for the next iteration
}
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(H), where H is the height of the tree ---> for recursion stack
*/
public static void flattenUsingRecursion(TreeNode root) {
if (root == null) return;
flattenUsingRecursion(root.left);
flattenUsingRecursion(root.right);
if (root.left == null) return;
TreeNode rightPart = root.right;
TreeNode leftPart = root.left;
TreeNode leftLeaf = root.left;
root.right = leftPart;
root.left = null;
while (leftLeaf.right != null) leftLeaf = leftLeaf.right;
leftLeaf.right = rightPart;
}
public static void flattenUsingRecursion2(TreeNode root) {
if (root == null) return;
flattenUsingRecursion2(root.left);
flattenUsingRecursion2(root.right);
TreeNode temp = root.right;
root.right = root.left;
root.left = null;
while (root.right != null) root = root.right;
root.right = temp;
}
public void flattenUsingRecursion3(TreeNode root) {
if (root == null) return;
TreeNode left = root.left, right = root.right;
root.left = null;
if (left != null) {
root.right = left;
}
flattenUsingRecursion3(left);
TreeNode leftMost = null;
while (left != null) {
leftMost = left;
left = left.right;
}
if (leftMost != null) leftMost.right = right;
else root.right = right;
flattenUsingRecursion3(right);
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(H), where H is the height of the tree ---> for recursion stack
*/
public static void flattenUsingDfs(TreeNode root) {
dfs(root);
}
private static TreeNode dfs(TreeNode node) {
if (node == null || node.left == null && node.right == null) return node;
TreeNode left = node.left;
TreeNode right = node.right;
node.left = null;
TreeNode leftMost = dfs(left);
TreeNode rightMost = dfs(right);
if (leftMost != null) {
node.right = left;
leftMost.right = right;
}
return rightMost != null ? rightMost : leftMost;
}
public static void flattenUsingDfs2(TreeNode root) {
helper(root);
}
private static TreeNode helper(TreeNode node) {
if (node == null || (node.left==null && node.right==null)) return node;
TreeNode rTemp = null;
TreeNode leaf = null;
// left child
if (node.left != null) {
rTemp = node.right;
node.right = node.left;
node.left = null;
}
leaf = helper(node.right);
// right child using temp
if (rTemp != null) {
leaf.right = rTemp;
leaf = helper(leaf.right);
}
return leaf;
}
public static void flattenUsingStack(TreeNode root) {
if (root == null) return;
class Pair<K, V> { final K key; final V value; Pair(K a, V b) { this.key = a; this.value = b;} public K getKey() { return this.key; } public V getValue() {return this.value;}}
int START = 1, END = 2;
TreeNode tailNode = null;
Stack<Pair<TreeNode, Integer>> stack = new Stack<>();
stack.push(new Pair<>(root, START));
while (!stack.isEmpty()) {
Pair<TreeNode, Integer> nodeData = stack.pop();
TreeNode currentNode = nodeData.getKey();
int recursionState = nodeData.getValue();
if (currentNode.left == null && currentNode.right == null) { // We reached a leaf node. Record this as a tail node and move on.
tailNode = currentNode;
continue;
}
if (recursionState == START) { // If the node is in the START state, it means we still haven't processed it's left child yet.
// If the current node has a left child, we add it
// to the stack AFTER adding the current node again
// to the stack with the END recursion state.
if (currentNode.left != null) {
stack.push(new Pair<>(currentNode, END));
stack.push(
new Pair<>(currentNode.left, START)
);
} else if (currentNode.right != null) {
stack.push(new Pair<>(currentNode.right, START)); // In case the current node didn't have a left child, we will add it's right child
}
} else {
// If the current node is in the END recursion state,
// that means we did process one of it's children. Left
// if it existed, else right.
TreeNode rightNode = currentNode.right;
// If there was a left child, there must have been a leaf
// node and so, we would have set the tailNode
if (tailNode != null) {
// Establish the proper connections.
tailNode.right = currentNode.right;
currentNode.right = currentNode.left;
currentNode.left = null;
rightNode = tailNode.right;
}
if (rightNode != null) {
stack.push(new Pair<>(rightNode, START));
}
}
}
}
private static TreeNode buildTree() {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(5);
root.left.left = new TreeNode(3);
root.left.right = new TreeNode(4);
root.right.right = new TreeNode(6);
return root;
}
private static void printTree(TreeNode root) {
System.out.print("\nTree: ");
java.util.Queue<TreeNode> queue = new java.util.LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode node = queue.poll();
System.out.print(node == null ? "null " : node.val + " ");
if (node == null) continue;
queue.add(node.left);
queue.add(node.right);
}
}
}
}