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

Commit7fb9c68

Browse files
refactor 545
1 parent140027b commit7fb9c68

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

‎src/main/java/com/fishercoder/solutions/_545.java

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
/**
99
* 545. Boundary of Binary Tree
10+
*
1011
* Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root.
1112
* Boundary includes left boundary, addLeaves, and right boundary in order without duplicate nodes.
1213
* Left boundary is defined as the path from root to the left-most node.
@@ -58,56 +59,58 @@
5859
5960
*/
6061
publicclass_545 {
61-
publicList<Integer>boundaryOfBinaryTree(TreeNoderoot) {
62-
List<Integer>nodes =newArrayList<>();
63-
if (root ==null) {
64-
returnnodes;
65-
}
66-
67-
nodes.add(root.val);
68-
leftBoundary(root.left,nodes);
69-
addLeaves(root.left,nodes);
70-
addLeaves(root.right,nodes);
71-
rightBoundary(root.right,nodes);
72-
returnnodes;
73-
}
62+
publicstaticclassSolution1 {
63+
publicList<Integer>boundaryOfBinaryTree(TreeNoderoot) {
64+
List<Integer>nodes =newArrayList<>();
65+
if (root ==null) {
66+
returnnodes;
67+
}
7468

75-
publicvoidleftBoundary(TreeNoderoot,List<Integer>nodes) {
76-
if (root ==null || (root.left ==null &&root.right ==null)) {
77-
/**we don't want to add any LEAVES in leftBoundary and rightBoundary functions either,
78-
* that's why we have the later condition in the if branch.*/
79-
return;
80-
}
81-
nodes.add(root.val);// add BEFORE child visit
82-
if (root.left ==null) {
83-
leftBoundary(root.right,nodes);
84-
}else {
69+
nodes.add(root.val);
8570
leftBoundary(root.left,nodes);
71+
addLeaves(root.left,nodes);
72+
addLeaves(root.right,nodes);
73+
rightBoundary(root.right,nodes);
74+
returnnodes;
8675
}
87-
}
8876

89-
publicvoidrightBoundary(TreeNoderoot,List<Integer>nodes) {
90-
if (root ==null || (root.right ==null &&root.left ==null)) {
91-
return;
92-
}
93-
if (root.right ==null) {
94-
rightBoundary(root.left,nodes);
95-
}else {
96-
rightBoundary(root.right,nodes);
77+
publicvoidleftBoundary(TreeNoderoot,List<Integer>nodes) {
78+
if (root ==null || (root.left ==null &&root.right ==null)) {
79+
/**we don't want to add any LEAVES in leftBoundary and rightBoundary functions either,
80+
* that's why we have the later condition in the if branch.*/
81+
return;
82+
}
83+
nodes.add(root.val);// add BEFORE child visit
84+
if (root.left ==null) {
85+
leftBoundary(root.right,nodes);
86+
}else {
87+
leftBoundary(root.left,nodes);
88+
}
9789
}
98-
nodes.add(root.val);// add AFTER child visit(reverse)
99-
}
10090

101-
publicvoidaddLeaves(TreeNoderoot,List<Integer>nodes) {
102-
if (root ==null) {
103-
return;
91+
publicvoidrightBoundary(TreeNoderoot,List<Integer>nodes) {
92+
if (root ==null || (root.right ==null &&root.left ==null)) {
93+
return;
94+
}
95+
if (root.right ==null) {
96+
rightBoundary(root.left,nodes);
97+
}else {
98+
rightBoundary(root.right,nodes);
99+
}
100+
nodes.add(root.val);// add AFTER child visit(reverse)
104101
}
105-
if (root.left ==null &&root.right ==null) {
106-
nodes.add(root.val);
107-
return;
102+
103+
publicvoidaddLeaves(TreeNoderoot,List<Integer>nodes) {
104+
if (root ==null) {
105+
return;
106+
}
107+
if (root.left ==null &&root.right ==null) {
108+
nodes.add(root.val);
109+
return;
110+
}
111+
addLeaves(root.left,nodes);
112+
addLeaves(root.right,nodes);
108113
}
109-
addLeaves(root.left,nodes);
110-
addLeaves(root.right,nodes);
111114
}
112115

113116
}

‎src/test/java/com/fishercoder/_545Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@
1212
importstaticjunit.framework.Assert.assertEquals;
1313

1414
publicclass_545Test {
15-
privatestatic_545test;
15+
privatestatic_545.Solution1test;
1616
privatestaticTreeNoderoot;
1717
privatestaticList<Integer>expected;
1818

1919
@BeforeClass
2020
publicstaticvoidsetup() {
21-
test =new_545();
21+
test =new_545.Solution1();
2222
}
2323

2424
@Test

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp