-
Notifications
You must be signed in to change notification settings - Fork 199
/
Copy pathProgram.java
84 lines (69 loc) · 1.98 KB
/
Program.java
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
package AlgoExSolutions.VeryHard.FlattenBinaryTree;
// import java.util.*;
/**
* * Flatten Binary Tree
*/
class Program {
/**
* * TC: O(n)
* * SC: O(d)
*/
public static BinaryTree flattenBinaryTree(BinaryTree root) {
// Write your code here.
return flattenBinaryTreeHelper(root)[0];
}
private static BinaryTree[] flattenBinaryTreeHelper(BinaryTree root) {
BinaryTree leftNode = null, rightNode = null;
if (root.left == null) leftNode = root;
else {
BinaryTree[] lstNodes = flattenBinaryTreeHelper(root.left);
connectNodes(lstNodes[1], root);
leftNode = lstNodes[0];
}
if (root.right == null) rightNode = root;
else {
BinaryTree[] rstNodes = flattenBinaryTreeHelper(root.right);
connectNodes(root, rstNodes[0]);
rightNode = rstNodes[1];
}
return new BinaryTree[] {leftNode, rightNode};
}
private static void connectNodes(BinaryTree left, BinaryTree right) {
left.right = right;
right.left = left;
}
/**
* * TC: O(n)
* * SC: O(n)
*/
// public static BinaryTree flattenBinaryTree(BinaryTree root) {
// List<BinaryTree> nodes = getNodes(root);
// for (int i = 1; i < nodes.size(); i++) {
// BinaryTree leftNode = nodes.get(i - 1), rightNode = nodes.get(i);
// leftNode.right = rightNode;
// rightNode.left = leftNode;
// }
// return nodes.get(0);
// }
// private static List<BinaryTree> getNodes(BinaryTree root) {
// List<BinaryTree> nodes = new ArrayList<>();
// getNodes(root, nodes);
// return nodes;
// }
// private static void getNodes(BinaryTree root, List<BinaryTree> nodes) {
// if (root != null) {
// getNodes(root.left, nodes);
// nodes.add(root);
// getNodes(root.right, nodes);
// }
// }
// This is the class of the input root. Do not edit it.
static class BinaryTree {
int value;
BinaryTree left = null;
BinaryTree right = null;
public BinaryTree(int value) {
this.value = value;
}
}
}