题解 | #二叉树中和为某一值的路径(一)#
二叉树中和为某一值的路径(一)
http://www.nowcoder.com/practice/508378c0823c423baa723ce448cbfd0c
python递归
class Solution:
def hasPathSum(self , root: TreeNode, sum: int) -> bool:
# write code here
if not root: return False
elif not root.left and not root.right:
return True if root.val==sum else False
else:
return self.hasPathSum(root.left,sum-root.val) or self.hasPathSum(root.right,sum-root.val)
题解-数据结构与算法 文章被收录于专栏
小菜鸟的题解