Skip to content

Commit fc8127f

Browse files
authored
Create Path Sum
1 parent eacd882 commit fc8127f

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

java/Tree/Path Sum

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public boolean hasPathSum(TreeNode root, int targetSum) {
18+
if (root == null) return false;
19+
if(root.left == null && root.right == null && root.val == targetSum) return true;
20+
return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
21+
}
22+
23+
}

0 commit comments

Comments
 (0)