Skip to content

Commit 806acb3

Browse files
committed
[Leo] 3rd Week solutions <Typo fixed, code manipulation>
1 parent e5a39a6 commit 806acb3

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

climbing-stairs/Leo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ def climbStairs(self, n: int) -> int:
99

1010
return fast
1111

12-
## TC: O(n), SC(1)
12+
## TC: O(n), SC:O(1)

meeting-rooms/Leo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
88

99
return True
1010

11-
## TC: n(nlogn), SC: O(1)
11+
## TC: O(nlogn), SC: O(1)

same-tree/Leo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
99
if p and q:
1010
return p.val == q.val and self.isSameTree(p.right, q.right) and self.isSameTree(p.left, q.left)
1111
else:
12-
return p == q
12+
return p is None and q is None
1313

1414
## TC: O(n), SC: O(n) or O(logn) if it is balanced

subtree-of-another-tree/Leo.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,25 @@
66
# self.right = right
77
class Solution:
88
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
9-
def dfs(root, subroot):
10-
if not root and not subroot:
9+
10+
def dfs(root, subRoot):
11+
if not root and not subRoot:
1112
return True
12-
if not root or not subroot:
13+
if not root or not subRoot:
1314
return False
14-
if root.val != subroot.val:
15+
if root.val != subRoot.val:
1516
return False
1617

17-
return dfs(root.left, subroot.left) and dfs(root.right, subroot.right)
18+
return dfs(root.left, subRoot.left) and dfs(root.right, subRoot.right)
1819

19-
def solve(root, subroot):
20+
def solve(root):
2021
if not root:
2122
return False
22-
if dfs(root, subroot):
23+
if dfs(root, subRoot):
2324
return True
24-
return solve(root.left, subroot) or solve(root.right, subroot)
25+
return solve(root.left) or solve(root.right)
2526

26-
return solve(root, subRoot)
27+
return solve(root)
2728

2829
## TC: O(mn), where m and n denote len(subroot) and len(root)
2930
## SC: O(m+n)

0 commit comments

Comments
 (0)