-
Notifications
You must be signed in to change notification settings - Fork 43
/
path-sum.py
148 lines (124 loc) · 4.15 KB
/
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
112. Path Sum
Easy
Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
A leaf is a node with no children.
Example 1:
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.
Example 2:
Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.
Example 3:
Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.
Constraints:
The number of nodes in the tree is in the range [0, 5000].
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
"""
# V0
# IDEA : DFS
class Solution(object):
def hasPathSum(self, root, sum):
if not root:
return False
if not root.left and not root.right:
return True if sum == root.val else False
else:
return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val)
# V1
# https://blog.csdn.net/coder_orz/article/details/51595815
# IDEA : DFS (recursion)
class Solution(object):
def hasPathSum(self, root, sum):
if not root:
return False
if not root.left and not root.right:
return True if sum == root.val else False
else:
return self.hasPathSum(root.left, sum-root.val) or self.hasPathSum(root.right, sum-root.val)
# V1'
# https://blog.csdn.net/coder_orz/article/details/51595815
# IDEA : DFS (non - recursion) (via stack)
class Solution(object):
def hasPathSum(self, root, sum):
"""
:type root: TreeNode
:type sum: int
:rtype: bool
"""
stack = [(root, sum)]
while len(stack) > 0:
node, tmp_sum = stack.pop() # make node = root, and tmp_sum = sum
if node:
if not node.left and not node.right and node.val == tmp_sum:
return True
stack.append((node.right, tmp_sum-node.val))
stack.append((node.left, tmp_sum-node.val))
return False
# V1''
# https://blog.csdn.net/coder_orz/article/details/51595815
# IDEA : BFS (via queue)
# 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
"""
queue = [(root, sum)]
while len(queue) > 0:
node, tmp_sum = queue.pop()
if node:
if not node.left and not node.right and node.val == tmp_sum:
return True
queue.insert(0, (node.right, tmp_sum-node.val))
queue.insert(0, (node.left, tmp_sum-node.val))
return False
# V1'''
# https://www.jiuzhang.com/solution/path-sum/#tag-highlight-lang-python
class Solution:
"""
@param root: the tree
@param sum: the sum
@return: if the tree has a root-to-leaf path
"""
def pathSum(self, root, sum):
# Write your code here.
if root == None:
return False;
elif (root.val == sum and root.left == None and root.right == None):
return True;
else:
return self.pathSum(root.left, sum - root.val) or self.pathSum(root.right, sum - root.val)
# V2
# Time: O(n)
# Space: O(h), h is height of binary tree
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
# @param root, a tree node
# @param sum, an integer
# @return a boolean
def hasPathSum(self, root, sum):
if root is None:
return False
if root.left is None and root.right is None and root.val == sum:
return True
return self.hasPathSum(root.left, sum - root.val) or self.hasPathSum(root.right, sum - root.val)