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

Commit20f20c6

Browse files
refactor 404
1 parentecd7f7b commit20f20c6

File tree

1 file changed

+28
-23
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+28
-23
lines changed

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

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
packagecom.fishercoder.solutions;
22

33
importcom.fishercoder.common.classes.TreeNode;
4-
/**Find the sum of all left leaves in a given binary tree.
4+
/**
5+
* 404. Sum of Left Leaves
6+
*
7+
* Find the sum of all left leaves in a given binary tree.
58
69
Example:
710
@@ -13,33 +16,35 @@
1316
1417
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.*/
1518
publicclass_404 {
16-
publicintsumOfLeftLeaves(TreeNoderoot) {
17-
intresult =0;
18-
if (root ==null) {
19-
returnresult;
19+
publicstaticclassSolution1 {
20+
publicintsumOfLeftLeaves(TreeNoderoot) {
21+
intresult =0;
22+
if (root ==null) {
23+
returnresult;
24+
}
25+
returndfs(root,result,false);
2026
}
21-
returndfs(root,result,false);
22-
}
2327

24-
privateintdfs(TreeNoderoot,intresult,booleanleft) {
25-
if (root.left ==null &&root.right ==null &&left) {
26-
result +=root.val;
27-
returnresult;
28-
}
29-
intleftResult =0;
30-
if (root.left !=null) {
31-
left =true;
32-
leftResult =dfs(root.left,result,left);
33-
}
34-
intrightResult =0;
35-
if (root.right !=null) {
36-
left =false;
37-
rightResult =dfs(root.right,result,left);
28+
privateintdfs(TreeNoderoot,intresult,booleanleft) {
29+
if (root.left ==null &&root.right ==null &&left) {
30+
result +=root.val;
31+
returnresult;
32+
}
33+
intleftResult =0;
34+
if (root.left !=null) {
35+
left =true;
36+
leftResult =dfs(root.left,result,left);
37+
}
38+
intrightResult =0;
39+
if (root.right !=null) {
40+
left =false;
41+
rightResult =dfs(root.right,result,left);
42+
}
43+
returnleftResult +rightResult;
3844
}
39-
returnleftResult +rightResult;
4045
}
4146

42-
privateclassSolution2 {
47+
publicstaticclassSolution2 {
4348

4449
publicintsumOfLeftLeaves(TreeNoderoot) {
4550
intsum =0;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp