-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProblem1630.java
95 lines (78 loc) · 1.95 KB
/
Problem1630.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
85
86
87
88
89
90
91
92
93
94
95
import java.util.Arrays;
/**
*
* This problem was asked by Facebook.
*
* Given a binary tree, return all paths from the root to leaves.
*
* For example, given the tree:
*
* 1
* / \
* 2 3
* / \
* 4 5
* Return [[1, 2], [1, 3, 4], [1, 3, 5]].
*
*/
public class Problem1630 {
public static void main(String[] args) {
Node node5 = new Node(5);
Node node4 = new Node(4);
Node node3 = new Node(3, node4, node5);
Node node2 = new Node(2);
Node root = new Node(1, node2, node3);
int[][] paths = getPaths(root);
System.out.println(Arrays.deepToString(paths));
}
public static int[][] getPaths(Node root) {
return getPaths(root, 1);
}
public static int[][] getPaths(Node node, int height) {
int[][] rightPaths = new int[][] {};
int[][] leftPaths = new int[][] {};
int[][] myPaths = new int[][] {};
if (node.right != null) {
rightPaths = getPaths(node.right, height + 1);
for (int[] rightPath: rightPaths) {
rightPath[height - 1] = node.data;
}
}
if (node.left != null) {
leftPaths = getPaths(node.left, height + 1);
for (int[] leftPath : leftPaths) {
leftPath[height - 1] = node.data;
}
}
if (node.left == null && node.right == null) {
// I am the leaf node
myPaths = new int[1][height];
myPaths[0][height - 1] = node.data;
}
int[][] allPaths = new int[rightPaths.length + leftPaths.length + myPaths.length][];
int i = 0;
for (int[] rightPath: rightPaths) {
allPaths[i++] = rightPath;
}
for (int[] leftPath : leftPaths) {
allPaths[i++] = leftPath;
}
for (int[] myPath : myPaths) {
allPaths[i++] = myPath;
}
return allPaths;
}
}
class Node {
int data;
Node left;
Node right;
Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
Node(int data) {
this(data, null, null);
}
}