Skip to content

Commit 956fc7b

Browse files
committed
Create 0112-path-sum.py
1 parent 14d058e commit 956fc7b

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

python/0112-path-sum.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def hasPathSum(self, root, sum):
3+
if not root:
4+
return False
5+
de = [
6+
(root, sum - root.val),
7+
]
8+
while de:
9+
node, curr_sum = de.pop()
10+
if not node.left and not node.right and curr_sum == 0:
11+
return True
12+
if node.right:
13+
de.append((node.right, curr_sum - node.right.val))
14+
if node.left:
15+
de.append((node.left, curr_sum - node.left.val))
16+
return False

0 commit comments

Comments
 (0)