-
Notifications
You must be signed in to change notification settings - Fork 45
/
_655.java
73 lines (64 loc) · 1.94 KB
/
_655.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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* LeetCode 655 - Print Binary Tree
* <p>
* DFS approach
* <p>
* Based on the problem description, let f(i) denote the width of the printed matrix w.r.t. the subtree rooted at i.
* Then, we have
* <p>
* f(i) = max(f(i.left), f(i.right)) * 2 + 1, the problem requires that the left and the right subtree must have matrices in same size.
* f(null) = 0
* <p>
* It is easy to see f(i) solves to f(i) = 2^f(height(i)) - 1.
* So, if the final matrix has h rows, it will have 2^h - 1 columns.
*/
public class _655 {
String[][] res;
public List<List<String>> printTree(TreeNode root) {
if (root == null) {
return Collections.EMPTY_LIST;
}
int h = dfs(root);
res = new String[h][(1 << h) - 1];
for (String[] row : res) {
Arrays.fill(row, "");
}
dfs(root, 0, 0, (1 << h) - 1 - 1);
List<List<String>> list = new ArrayList<>(h);
for (int i = 0; i < h; i++) {
list.add(Arrays.asList(res[i]));
}
return list;
}
private int dfs(TreeNode root) {
if (root == null) {
return 0;
} else {
return Math.max(dfs(root.left), dfs(root.right)) + 1;
}
}
/**
* @param col1 - inclusive
* @param col2 - inclusive
*/
private void dfs(TreeNode root, int row, int col1, int col2) {
if (root != null) {
int mid = (col1 + col2) / 2;
res[row][mid] = "" + root.val;
dfs(root.left, row + 1, col1, mid - 1);
dfs(root.right, row + 1, mid + 1, col2);
}
}
public static void main(String[] args) {
_655 sol = new _655();
TreeNode a = new TreeNode(1);
a.left = new TreeNode(2);
for (List<String> row : sol.printTree(a)) {
System.out.println(row);
}
}
}