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

Commit51ce8af

Browse files
refactor 437
1 parent91bb8ce commit51ce8af

File tree

1 file changed

+13
-9
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+13
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
importcom.fishercoder.common.classes.TreeNode;
44

55
/**
6+
* 437. Path Sum III
7+
*
68
* You are given a binary tree in which each node contains an integer value.
79
810
Find the number of paths that sum to a given value.
@@ -32,18 +34,20 @@ The path does not need to start or end at the root or a leaf, but it must go dow
3234

3335
publicclass_437 {
3436

35-
publicintpathSum(TreeNoderoot,intsum) {
36-
if (root ==null) {
37-
return0;
37+
publicstaticclassSolution1 {
38+
publicintpathSum(TreeNoderoot,intsum) {
39+
if (root ==null) {
40+
return0;
41+
}
42+
returnpathSumFrom(root,sum) +pathSum(root.left,sum) +pathSum(root.right,sum);
3843
}
39-
returnpathSumFrom(root,sum) +pathSum(root.left,sum) +pathSum(root.right,sum);
40-
}
4144

42-
privateintpathSumFrom(TreeNoderoot,intsum) {
43-
if (root ==null) {
44-
return0;
45+
privateintpathSumFrom(TreeNoderoot,intsum) {
46+
if (root ==null) {
47+
return0;
48+
}
49+
return (root.val ==sum ?1 :0) +pathSumFrom(root.left,sum -root.val) +pathSumFrom(root.right,sum -root.val);
4550
}
46-
return (root.val ==sum ?1 :0) +pathSumFrom(root.left,sum -root.val) +pathSumFrom(root.right,sum -root.val);
4751
}
4852

4953
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp