Print a binary tree in an m*n 2D string array following these rules:
- The row number
m
should be equal to the height of the given binary tree. - The column number
n
should always be an odd number. - 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.
- Each unused space should contain an empty string
""
. - Print the subtrees following the same rules.
Example 1:
Input:
1
/
2
Output:
[["", "1", ""],
["2", "", ""]]
Example 2:
Input:
1
/ \
2 3
\
4
Output:
[["", "", "", "1", "", "", ""],
["", "2", "", "", "", "3", ""],
["", "", "4", "", "", "", ""]]
Example 3:
Input:
1
/ \
2 5
/
3
/
4
Output:
[["", "", "", "", "", "", "", "1", "", "", "", "", "", "", ""]
["", "", "", "2", "", "", "", "", "", "", "", "5", "", "", ""]
["", "3", "", "", "", "", "", "", "", "", "", "", "", "", ""]
["4", "", "", "", "", "", "", "", "", "", "", "", "", "", ""]]
Note:The height of binary tree is in the range of [1, 10].
Solution 1: 先得到树高,确定row = height, column = pow(2, height) - 1。再level order traversal,根据root的位置和每层offset确定左右节点位置
public List<List<String>> printTree(TreeNode root) {
if (root == null) {
return new ArrayList<List<String>>();
}
int row = getHeight(root);
int column = (int)(Math.pow(2, row) - 1);
List<List<String>> ans = generateList(row, column);
Queue<TreeNode> queue = new LinkedList<>();
ans.get(0).set(column / 2, String.valueOf(root.val));
root.val = column / 2;
queue.offer(root);
int offset = root.val - root.val / 2, level = 1;
while (offset > 0) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
if (cur.left != null) {
ans.get(level).set(cur.val - offset, String.valueOf(cur.left.val));
cur.left.val = cur.val - offset;
queue.offer(cur.left);
}
if (cur.right != null) {
ans.get(level).set(cur.val + offset, String.valueOf(cur.right.val));
cur.right.val = cur.val + offset;
queue.offer(cur.right);
}
}
offset /= 2;
level++;
}
return ans;
}
private int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(getHeight(root.left), getHeight(root.right)) + 1;
}
private List<List<String>> generateList(int m, int n) {
List<List<String>> ans = new ArrayList<>();
for (int i = 0; i < m; i++) {
List<String> temp = new ArrayList<>();
for (int j = 0; j < n; j++) {
temp.add("");
}
ans.add(temp);
}
return ans;
}
Solution 2: recursion
public List<List<String>> printTree(TreeNode root) {
List<List<String>> res = new LinkedList<>();
int height = root == null ? 1 : getHeight(root);
int rows = height, columns = (int) (Math.pow(2, height) - 1);
List<String> row = new ArrayList<>();
for(int i = 0; i < columns; i++) row.add("");
for(int i = 0; i < rows; i++) res.add(new ArrayList<>(row));
populateRes(root, res, 0, rows, 0, columns - 1);
return res;
}
public void populateRes(TreeNode root, List<List<String>> res, int row, int totalRows, int i, int j) {
if (row == totalRows || root == null) return;
res.get(row).set((i+j)/2, Integer.toString(root.val));
populateRes(root.left, res, row+1, totalRows, i, (i+j)/2 - 1);
populateRes(root.right, res, row+1, totalRows, (i+j)/2+1, j);
}
public int getHeight(TreeNode root) {
if (root == null) return 0;
return 1 + Math.max(getHeight(root.left), getHeight(root.right));
}