-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLeetCode-655-Print-Binary-Tree.java
More file actions
90 lines (77 loc) · 3.15 KB
/
LeetCode-655-Print-Binary-Tree.java
File metadata and controls
90 lines (77 loc) · 3.15 KB
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
// 1. Recursive
/*
https://leetcode.com/problems/print-binary-tree/discuss/106239/Java-Recursive-Solution
*/
// public List<List<String>> printTree(TreeNode root) {
// int row = getHeight(root);
// int col = (int) Math.pow(2, row) - 1;
// List<List<String>> res = new ArrayList<>();
// List<String> ans = new ArrayList<>();
// for (int i = 0; i < col; i++) ans.add("");
// for (int i = 0; i < row; i++) res.add(new ArrayList<>(ans));
// populateResult(root, res, 0, row, 0, col - 1);
// return res;
// }
// private void populateResult(TreeNode root, List<List<String>> res, int currRow, int totalRow, int i, int j) {
// if (root == null || currRow == totalRow) return; // we reached the last line or we reached a null node
// int idx = (i + j) / 2;
// res.get(currRow).set(idx, String.valueOf(root.val));
// populateResult(root.left, res, currRow + 1, totalRow, i, idx - 1);
// populateResult(root.right, res, currRow + 1, totalRow, idx + 1, j);
// }
// private int getHeight(TreeNode root) {
// if (root == null) return 0;
// return 1 + Math.max(getHeight(root.left), getHeight(root.right));
// }
// 2. BFS (Level Order Traversal)
/*
https://leetcode.com/problems/print-binary-tree/discuss/106269/Java-Iterative-Level-Order-Traversal-with-Queue
*/
public List<List<String>> printTree(TreeNode root) {
int row = getHeight(root);
int col = (int) Math.pow(2, row) - 1;
List<List<String>> res = new ArrayList<>();
List<String> ans = new ArrayList<>();
for (int i = 0; i < col; i++) ans.add("");
for (int i = 0; i < row; i++) res.add(new ArrayList<>(ans));
Queue<TreeNode> queue = new LinkedList<>();
Queue<int[]> indexQ = new LinkedList<>();
queue.offer(root);
indexQ.offer(new int[]{0, col - 1});
int level = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode curr = queue.poll();
int[] indices = indexQ.poll();
int l = indices[0], r = indices[1];
int mid = l + (r - l) / 2;
res.get(level).set(mid, String.valueOf(curr.val));
if (curr.left != null) {
queue.offer(curr.left);
indexQ.offer(new int[] {l, mid - 1});
}
if (curr.right != null) {
queue.offer(curr.right);
indexQ.offer(new int[] {mid + 1, r});
}
}
level++;
}
return res;
}
private int getHeight(TreeNode root) {
if (root == null) return 0;
return 1 + Math.max(getHeight(root.left), getHeight(root.right));
}
}