Skip to content

Commit a149a4b

Browse files
committed
Create 112. Path Sum
Signed-off-by: Tahsin Tunan <[email protected]>
1 parent 1bc9a15 commit a149a4b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

go/0112-path-sum.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
10+
func hasPathSum(root *TreeNode, targetSum int) bool {
11+
if root == nil {
12+
return false
13+
}
14+
if isChild(root) && targetSum == root.Val {
15+
return true
16+
}
17+
if hasPathSum(root.Left, targetSum-root.Val) || hasPathSum(root.Right, targetSum-root.Val) {
18+
return true
19+
}
20+
return false
21+
}
22+
23+
func isChild(node *TreeNode) bool {
24+
return node.Left == nil && node.Right == nil
25+
}

0 commit comments

Comments
 (0)