Skip to content

Commit eb2bbf4

Browse files
committed
Sync LeetCode submission Runtime - 3 ms (29.32%), Memory - 19 MB (13.20%)
1 parent 9c50821 commit eb2bbf4

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

0112-path-sum/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<pre>
1717
<strong>Input:</strong> root = [1,2,3], targetSum = 5
1818
<strong>Output:</strong> false
19-
<strong>Explanation:</strong> There two root-to-leaf paths in the tree:
19+
<strong>Explanation:</strong> There are two root-to-leaf paths in the tree:
2020
(1 --&gt; 2): The sum is 3.
2121
(1 --&gt; 3): The sum is 4.
2222
There is no root-to-leaf path with sum = 5.

0112-path-sum/solution.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
# Approach 1 - Recursion
1+
# Approach 1: Recursion
22

3-
# Time: O(N)
4-
# Space: O(N) in worst case, O(log N) in best case
3+
# Time: O(n)
4+
# Space: O(n) in worst base, O(log n) in best case
55

66
# Definition for a binary tree node.
77
# class TreeNode:
88
# def __init__(self, val=0, left=None, right=None):
99
# self.val = val
1010
# self.left = left
1111
# self.right = right
12-
1312
class Solution:
1413
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
1514
if not root:
1615
return False
17-
16+
1817
targetSum -= root.val
19-
if not root.left and not root.right:
18+
19+
if not root.left and not root.right: # reached leaf
2020
return targetSum == 0
21-
21+
2222
return self.hasPathSum(root.left, targetSum) or self.hasPathSum(root.right, targetSum)
2323

0 commit comments

Comments
 (0)