-
Notifications
You must be signed in to change notification settings - Fork 0
/
112.path-sum.py
48 lines (44 loc) · 1.34 KB
/
112.path-sum.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#
# @lc app=leetcode id=112 lang=python3
#
# [112] Path Sum
#
# @lc code=start
# 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, targetSum: int) -> bool:
results = []
def dfs(node, path):
if not node.left and not node.right:
path.append(node.val)
results.append(path)
if node.left:
dfs(node.left, path+[node.val])
if node.right:
dfs(node.right, path+[node.val])
if not root:
return
dfs(root, [])
for result in results:
if targetSum == sum(result):
return True
return False
def hasPathSum2(self, root, targetSum: int) -> bool:
if not root: return
if not root.left and not root.right and root.val == sum: return True
sum -= root.val
return self.hasPathSum2(root.left, sum) or self.hasPathSum2(root.right, sum)
# @lc code=end
a = TreeNode(3)
b = TreeNode(2, left=None, right=a)
c = TreeNode(-10)
d = TreeNode(-2, left=None, right=None)
# d = TreeNode(-5, left=c, right=d)
root = TreeNode(1, left=d, right=b)
pro = Solution()
print(pro.hasPathSum(root, -1))