add new solutions
This commit is contained in:
parent
e1b702657c
commit
59b71480d4
11 changed files with 343 additions and 0 deletions
26
solutions/1/q112/solution.go
Normal file
26
solutions/1/q112/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q112
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func traverse(node *TreeNode, tgtSum, curSum int) bool {
|
||||
if node == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
sum := curSum + node.Val
|
||||
if node.Left == nil && node.Right == nil && sum == tgtSum {
|
||||
return true
|
||||
}
|
||||
return traverse(node.Left, tgtSum, sum) ||
|
||||
traverse(node.Right, tgtSum, sum)
|
||||
}
|
||||
|
||||
func hasPathSum(root *TreeNode, targetSum int) bool {
|
||||
return traverse(root, targetSum, 0)
|
||||
}
|
||||
|
||||
var _ = hasPathSum
|
||||
Loading…
Add table
Add a link
Reference in a new issue