# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def hasPathSum(self, root: TreeNode, targetSum: int) -> bool:
vals = []
def dfs(root,val):
if root is None:
return 0
else:
val += root.val
if root.left is None and root.right is None:
vals.append(val)
return 0
if root.left:
dfs(root.left, val)
if root.right:
dfs(root.right, val)
dfs(root,0)
return vals
猜你喜欢:👇🏻
⭐【Leetcode】输出二叉树的所有路径(数值型)
⭐【Leetcode】Python 代码本地构造二叉树、链表
⭐【Leetcode】组合、排列、子集、切割(回溯模板和去重方法)