给你二叉树的根节点 root 和一个表示目标和的整数 targetSum ,判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。
叶子节点 是指没有子节点的节点。
示例 1:
输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出:true
示例 2:
输入:root = [1,2,3], targetSum = 5
输出:false
示例 3:
输入:root = [1,2], targetSum = 0
输出:false
提示:
树中节点的数目在范围 [0, 5000] 内
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/path-sum
参考:
python
# 112.路径之和
class Solution:
def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
def isOrNot(root, targetSum) -> bool:
# 叶子结点且路径和满足
if not root.left and not root.right and targetSum == 0:
return True
# 叶子结点但路径和不满足
if not root.left and not root.right:
return False
if root.left:
targetSum -= root.left.val
if isOrNot(root.left, targetSum):
return True
targetSum += root.left.val # 回溯
if root.right:
targetSum -= root.right.val
if isOrNot(root.right, targetSum):
return True
targetSum += root.right.val # 回溯
return False
if root == None:
return False
else:
return isOrNot(root, targetSum-root.val)
class Solution2:
def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
"""
迭代
:param root:
:param targetSum:
:return:
"""
if not root:
return False
stack = []
stack.append((root, root.val))
while stack:
curNode, pathSum = stack.pop()
if not curNode.left and not curNode.right and pathSum == targetSum:
return True
if curNode.right:
stack.append((curNode.right, pathSum+curNode.right.val))
if curNode.left:
stack.append((curNode.left, pathSum+curNode.left.val))
return False
# 113.路径之和II
class Solution3:
def pathsum(self, root: TreeNode, targetSUm: int) -> [[int]]:
def traversal(curNode, remain):
if not curNode.left and not curNode.right and remain == 0:
res.append(path[:])
return
if not curNode.left and not curNode.right:
return
if curNode.left:
path.append(curNode.left.val)
remain -= curNode.left.val
traversal(curNode.left, remain)
path.pop()
remain += curNode.left.val
if curNode.right:
path.append(curNode.right.val)
remain -= curNode.right.val
traversal(curNode.right, remain)
path.pop()
remain += curNode.right.val
res, path = [], []
if not root:
return []
path.append(root.val)
traversal(root, targetSUm-root.val)
return res
golang
package binaryTree
import "fmt"
// 112.pathsum
// 递归法
func hasPathSum(root *TreeNode, targetSum int) bool {
var flag bool
if root == nil {
return flag
}
pathSum(root, 0, targetSum, &flag)
return flag
}
func pathSum(root *TreeNode, sum int, targetSum int, flag *bool) {
sum += root.Val
if root.Left == nil && root.Right == nil && sum == targetSum {
*flag = true
return
}
if root.Left != nil && !(*flag) {
pathSum(root.Left, sum, targetSum, flag)
}
if root.Right != nil && !(*flag) {
pathSum(root.Right, sum, targetSum, flag)
}
}
// 113-递归法
func pathSUm(root *TreeNode, targetSum int) [][]int {
var res [][]int
if root == nil {
return res
}
// 路径节点值集合
var sumNodes []int
hasPathSum1(root, &sumNodes, targetSum, &res)
return res
}
func hasPathSum1(root *TreeNode, sumNodes *[]int, targetSum int, res *[][]int) {
*sumNodes = append(*sumNodes, root.Val)
if root.Left ==nil && root.Right == nil {
fmt.Println(*sumNodes)
var sum int
var num int
for k,v := range *sumNodes {
sum += v
num = k
}
//新的nodes接受指针里的值,防止最终指针里的值发生变动,导致最后的结果都是最后一个sumnodes的值
tmpNodes := make([]int, num+1)
for k,v := range *sumNodes {
tmpNodes[k] = v
}
if sum == targetSum {
*res = append(*res, tmpNodes)
}
}
if root.Left != nil {
hasPathSum1(root.Left, sumNodes, targetSum, res)
// 回溯
*sumNodes = (*sumNodes)[:len(*sumNodes)-1]
}
if root.Right != nil {
hasPathSum1(root.Right, sumNodes, targetSum, res)
// 回溯
*sumNodes = (*sumNodes)[:len(*sumNodes)-1]
}
}