Skip to content

Commit 6046afe

Browse files
authored
Merge pull request neetcode-gh#1369 from notauserx/Create-112-Path-Sum.cs
Create 112-Path-Sum.cs
2 parents 98b6666 + c588795 commit 6046afe

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

csharp/112-Path-Sum.cs

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

0 commit comments

Comments
 (0)