catch-up
This commit is contained in:
25
path-sum/sol.go
Normal file
25
path-sum/sol.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
/**
|
||||
* Definition for a binary tree node.
|
||||
* type TreeNode struct {
|
||||
* Val int
|
||||
* Left *TreeNode
|
||||
* Right *TreeNode
|
||||
* }
|
||||
*/
|
||||
func hasPathSum(root *TreeNode, targetSum int) bool {
|
||||
if root == nil {
|
||||
return false
|
||||
}
|
||||
if root.Left == nil && root.Right == nil {
|
||||
return targetSum == root.Val
|
||||
}
|
||||
return hasPathSum(root.Left, targetSum-root.Val) || hasPathSum(root.Right, targetSum-root.Val)
|
||||
}
|
Reference in New Issue
Block a user