-
标签:
树
-
难度:
简单
- 题目描述
![](https://img.haomeiwen.com/i9324289/4f3d769d121d4730.png)
- 我的解法
注意递归出口设计, easy~
。
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
if root == None:
return False
if not root.left and not root.right:
if root.val != sum:
return False
else:
return True
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)
- 其他解法
暂略。
网友评论