Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitebaf443

Browse files
refactor 655
1 parent48b128a commitebaf443

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
publicclass_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-
publicList<List<String>>printTree(TreeNoderoot) {
72-
List<List<String>>result =newLinkedList<>();
73-
intheight =root ==null ?1 :getHeight(root);
74-
introws =height;
75-
intcolumns = (int) (Math.pow(2,height) -1);
76-
List<String>row =newArrayList<>();
77-
for (inti =0;i <columns;i++) {
78-
row.add("");
79-
}
80-
for (inti =0;i <rows;i++) {
81-
result.add(newArrayList<>(row));
67+
publicstaticclassSolution1 {
68+
69+
/**
70+
* Reference: https://discuss.leetcode.com/topic/98381/java-recursive-solution
71+
* and https://leetcode.com/articles/print-binary-tree/
72+
*/
73+
publicList<List<String>>printTree(TreeNoderoot) {
74+
List<List<String>>result =newLinkedList<>();
75+
intheight =root ==null ?1 :getHeight(root);
76+
introws =height;
77+
intcolumns = (int) (Math.pow(2,height) -1);
78+
List<String>row =newArrayList<>();
79+
for (inti =0;i <columns;i++) {
80+
row.add("");
81+
}
82+
for (inti =0;i <rows;i++) {
83+
result.add(newArrayList<>(row));
84+
}
85+
populateResult(root,result,0,rows,0,columns -1);
86+
returnresult;
8287
}
83-
populateResult(root,result,0,rows,0,columns -1);
84-
returnresult;
85-
}
8688

87-
privatevoidpopulateResult(TreeNoderoot,List<List<String>>result,introw,inttotalRows,inti,intj) {
88-
if (row ==totalRows ||root ==null) {
89-
return;
89+
privatevoidpopulateResult(TreeNoderoot,List<List<String>>result,introw,inttotalRows,inti,intj) {
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-
privateintgetHeight(TreeNoderoot) {
97-
if (root ==null) {
98-
return0;
98+
privateintgetHeight(TreeNoderoot) {
99+
if (root ==null) {
100+
return0;
101+
}
102+
return1 +Math.max(getHeight(root.left),getHeight(root.right));
99103
}
100-
return1 +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
publicclass_655Test {
1717
privatestaticList<List<String>>expected;
1818
privatestaticTreeNoderoot;
19-
privatestatic_655test;
19+
privatestatic_655.Solution1solution1;
2020

2121
@BeforeClass
2222
publicstaticvoidsetup() {
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
publicvoidtest2() {
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
publicvoidtest3() {
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)

[8]ページ先頭

©2009-2025 Movatter.jp