655 Print Binary Tree

1. Question

Print a binary tree in an m*n 2D string array following these rules:
  1. 1.
    The row numbermshould be equal to the height of the given binary tree.
  2. 2.
    The column numbernshould always be an odd number.
  3. 3.
    The root node's value (in string format) should be put in the exactly middle of the first row it can be put. The column and the row where the root node belongs will separate the rest space into two parts (left-bottom part and right-bottom part). You should print the left subtree in the left-bottom part and print the right subtree in the right-bottom part. The left-bottom part and the right-bottom part should have the same size. Even if one subtree is none while the other is not, you don't need to print anything for the none subtree but still need to leave the space as large as that for the other subtree. However, if two subtrees are none, then you don't need to leave space for both of them.
  4. 4.
    Each unused space should contain an empty string"".
  5. 5.
    Print the subtrees following the same rules.
Example 1:
1
Input:
2
3
1
4
/
5
2
6
7
Output:
8
9
[["", "1", ""],
10
["2", "", ""]]
Copied!
Example 2:
1
Input:
2
3
1
4
/ \
5
2 3
6
\
7
4
8
9
Output:
10
11
[["", "", "", "1", "", "", ""],
12
["", "2", "", "", "", "3", ""],
13
["", "", "4", "", "", "", ""]]
Copied!
Example 3:
1
Input:
2
3
1
4
/ \
5
2 5
6
/
7
3
8
/
9
4
10
11
Output:
12
13
[["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""]
14
["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""]
15
["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""]
16
["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]
Copied!
Note:The height of binary tree is in the range of [1, 10].

2. Implementation

(1) DFS
1
class Solution {
2
public List<List<String>> printTree(TreeNode root) {
3
List<List<String>> res = new ArrayList<>();
4
5
if (root == null) {
6
return res;
7
}
8
9
int rows = getHeight(root);
10
int cols = (1 << rows) - 1;
11
String[][] table = new String[rows][cols];
12
13
for (String[] row : table) {
14
Arrays.fill(row, "");
15
}
16
17
fillTable(root, 0, 0, cols - 1, table);
18
19
for (String[] row : table) {
20
res.add(Arrays.asList(row));
21
}
22
return res;
23
}
24
25
public void fillTable(TreeNode node, int row, int leftCol, int rightCol, String[][] table) {
26
if (node == null) {
27
return;
28
}
29
30
int mid = leftCol + (rightCol - leftCol) / 2;
31
table[row][mid] = node.val + "";
32
33
fillTable(node.left, row + 1, leftCol, mid - 1, table);
34
fillTable(node.right, row + 1, mid + 1, rightCol, table);
35
}
36
37
public int getHeight(TreeNode node) {
38
if (node == null) {
39
return 0;
40
}
41
42
return 1 + Math.max(getHeight(node.left), getHeight(node.right));
43
}
44
}
Copied!
(1) BFS
1
class Solution {
2
class Cell {
3
TreeNode node;
4
int row;
5
int left;
6
int right;
7
8
public Cell(TreeNode node, int row, int left, int right) {
9
this.node = node;
10
this.row = row;
11
this.left = left;
12
this.right = right;
13
}
14
}
15
16
public List<List<String>> printTree(TreeNode root) {
17
List<List<String>> res = new ArrayList<>();
18
19
if (root == null) {
20
return res;
21
}
22
23
int rows = getHeight(root);
24
int cols = (1 << rows) - 1;
25
26
String[][] table = new String[rows][cols];
27
for (String[] row : table) {
28
Arrays.fill(row, "");
29
}
30
31
Queue<Cell> queue = new LinkedList<>();
32
queue.add(new Cell(root, 0, 0, cols - 1));
33
34
while (!queue.isEmpty()) {
35
Cell curCell = queue.remove();
36
int mid = curCell.left + (curCell.right - curCell.left) / 2;
37
table[curCell.row][mid] = curCell.node.val + "";
38
39
if (curCell.node.left != null) {
40
queue.add(new Cell(curCell.node.left, curCell.row + 1, curCell.left, mid - 1));
41
}
42
43
if (curCell.node.right != null) {
44
queue.add(new Cell(curCell.node.right, curCell.row + 1, mid + 1, curCell.right));
45
}
46
}
47
48
for (String[] row : table) {
49
res.add(Arrays.asList(row));
50
}
51
return res;
52
}
53
54
public int getHeight(TreeNode node) {
55
if (node == null) {
56
return 0;
57
}
58
59
return 1 + Math.max(getHeight(node.left), getHeight(node.right));
60
}
61
}
Copied!

3. Time & Space Complexity

DFS: 时间复杂度O(h * 2^h), 空间复杂度O(h * 2^h)
BFS: 时间复杂度O(h * 2^h), 空间复杂度O(h * 2^h)