Skip to content

Commit 6b0e779

Browse files
authored
Add files via upload
1 parent 075dc92 commit 6b0e779

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

PathSum.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//https://leetcode.com/problems/path-sum/
2+
public class PathSum {
3+
4+
public boolean hasPathSum(TreeNode root, int targetSum) {
5+
return pathSum(root,targetSum,0);
6+
}
7+
8+
public boolean pathSum(TreeNode root,int targetSum, int sum) {
9+
if(root!=null) {
10+
sum+=root.val;
11+
if(root.left==null && root.right==null && sum==targetSum) {
12+
return true;
13+
}
14+
return pathSum(root.left,targetSum,sum) || pathSum(root.right, targetSum, sum);
15+
}
16+
return false;
17+
}
18+
}

0 commit comments

Comments
 (0)