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

Commit9ff09b3

Browse files
refactor 112
1 parent72c44f2 commit9ff09b3

File tree

1 file changed

+20
-0
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+20
-0
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,24 @@ public boolean hasPathSum(TreeNode root, int sum) {
1414
returnhasPathSum(root.left,sum -root.val) ||hasPathSum(root.right,sum -root.val);
1515
}
1616
}
17+
18+
publicstaticclassSolution2 {
19+
/**
20+
* My completely original solution on 9/23/2021.
21+
*/
22+
publicbooleanhasPathSum(TreeNoderoot,inttargetSum) {
23+
returndfs(root,0,targetSum);
24+
}
25+
26+
privatebooleandfs(TreeNoderoot,intsum,inttargetSum) {
27+
if (root ==null) {
28+
returnfalse;
29+
}
30+
sum +=root.val;
31+
if (root.left ==null &&root.right ==null) {
32+
returnsum ==targetSum;
33+
}
34+
returndfs(root.left,sum,targetSum) ||dfs(root.right,sum,targetSum);
35+
}
36+
}
1737
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp