-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path112.py
More file actions
31 lines (29 loc) · 898 Bytes
/
112.py
File metadata and controls
31 lines (29 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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):
# 方法1
# result = []
# if root is None:
# return False
# def dfs(root, ans):
# if root.left is None and root.right is None:
# result.append(ans)
# if root.left:
# dfs(root.left, ans+root.left.val)
# if root.right:
# dfs(root.right, ans+root.right.val)
# dfs(root, root.val)
# if sum in result:
# return True
# return False
# 方法2
if root is None:
return False
if root.left is None and root.right is None:
return root.val == sum
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum-root.val)