Skip to content

Commit ebaf443

Browse files
refactor 655
1 parent 48b128a commit ebaf443

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

src/main/java/com/fishercoder/solutions/_655.java

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,39 +64,42 @@
6464
*/
6565
public class _655 {
6666

67-
/**
68-
* Reference: https://discuss.leetcode.com/topic/98381/java-recursive-solution
69-
* and https://leetcode.com/articles/print-binary-tree/
70-
*/
71-
public List<List<String>> printTree(TreeNode root) {
72-
List<List<String>> result = new LinkedList<>();
73-
int height = root == null ? 1 : getHeight(root);
74-
int rows = height;
75-
int columns = (int) (Math.pow(2, height) - 1);
76-
List<String> row = new ArrayList<>();
77-
for (int i = 0; i < columns; i++) {
78-
row.add("");
79-
}
80-
for (int i = 0; i < rows; i++) {
81-
result.add(new ArrayList<>(row));
67+
public static class Solution1 {
68+
69+
/**
70+
* Reference: https://discuss.leetcode.com/topic/98381/java-recursive-solution
71+
* and https://leetcode.com/articles/print-binary-tree/
72+
*/
73+
public List<List<String>> printTree(TreeNode root) {
74+
List<List<String>> result = new LinkedList<>();
75+
int height = root == null ? 1 : getHeight(root);
76+
int rows = height;
77+
int columns = (int) (Math.pow(2, height) - 1);
78+
List<String> row = new ArrayList<>();
79+
for (int i = 0; i < columns; i++) {
80+
row.add("");
81+
}
82+
for (int i = 0; i < rows; i++) {
83+
result.add(new ArrayList<>(row));
84+
}
85+
populateResult(root, result, 0, rows, 0, columns - 1);
86+
return result;
8287
}
83-
populateResult(root, result, 0, rows, 0, columns - 1);
84-
return result;
85-
}
8688

87-
private void populateResult(TreeNode root, List<List<String>> result, int row, int totalRows, int i, int j) {
88-
if (row == totalRows || root == null) {
89-
return;
89+
private void populateResult(TreeNode root, List<List<String>> result, int row, int totalRows, int i, int j) {
90+
if (row == totalRows || root == null) {
91+
return;
92+
}
93+
result.get(row).set((i + j) / 2, Integer.toString(root.val));
94+
populateResult(root.left, result, row + 1, totalRows, i, (i + j) / 2 - 1);
95+
populateResult(root.right, result, row + 1, totalRows, (i + j) / 2 + 1, j);
9096
}
91-
result.get(row).set((i + j) / 2, Integer.toString(root.val));
92-
populateResult(root.left, result, row + 1, totalRows, i, (i + j) / 2 - 1);
93-
populateResult(root.right, result, row + 1, totalRows, (i + j) / 2 + 1, j);
94-
}
9597

96-
private int getHeight(TreeNode root) {
97-
if (root == null) {
98-
return 0;
98+
private int getHeight(TreeNode root) {
99+
if (root == null) {
100+
return 0;
101+
}
102+
return 1 + Math.max(getHeight(root.left), getHeight(root.right));
99103
}
100-
return 1 + Math.max(getHeight(root.left), getHeight(root.right));
101104
}
102105
}

src/test/java/com/fishercoder/_655Test.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
public class _655Test {
1717
private static List<List<String>> expected;
1818
private static TreeNode root;
19-
private static _655 test;
19+
private static _655.Solution1 solution1;
2020

2121
@BeforeClass
2222
public static void setup() {
23-
test = new _655();
23+
solution1 = new _655.Solution1();
2424
}
2525

2626
@Test
@@ -38,21 +38,21 @@ public void test1() {
3838
row2.add("");
3939
expected.add(row2);
4040
CommonUtils.printListList(expected);
41-
assertEquals(expected, test.printTree(root));
41+
assertEquals(expected, solution1.printTree(root));
4242
}
4343

4444
@Test
4545
public void test2() {
4646
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 2, 3, null, 4));
4747
TreeUtils.printBinaryTree(root);
48-
CommonUtils.printListList(test.printTree(root));
48+
CommonUtils.printListList(solution1.printTree(root));
4949
}
5050

5151
@Test
5252
public void test3() {
5353
root = TreeUtils.constructBinaryTree(Arrays.asList(3, null, 30, 10, null, null, 15, null, 45));
5454
TreeUtils.printBinaryTree(root);
55-
CommonUtils.printListList(test.printTree(root));
56-
System.out.println(test.printTree(root));
55+
CommonUtils.printListList(solution1.printTree(root));
56+
System.out.println(solution1.printTree(root));
5757
}
5858
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy