Skip to content

Commit 230f47c

Browse files
authored
Merge pull request #2149 from mandel-17/main
[mandel-17] WEEK 04 Solutions
2 parents 62beb78 + 6a14065 commit 230f47c

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import collections
2+
from typing import Optional
3+
4+
class TreeNode:
5+
def __init__(self, val=0, left=None, right=None):
6+
self.val = val
7+
self.left = left
8+
self.right = right
9+
10+
class Solution:
11+
def maxDepth(self, root: Optional[TreeNode]) -> int:
12+
if root is None:
13+
return 0
14+
queue = collections.deque([root])
15+
depth = 0
16+
17+
while queue:
18+
depth += 1
19+
for _ in range(len(queue)):
20+
current_root = queue.popleft()
21+
if current_root.left:
22+
queue.append(current_root.left)
23+
if current_root.right:
24+
queue.append(current_root.right)
25+
return depth
26+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import Optional
2+
3+
class ListNode:
4+
def __init__(self, val=0, next=None):
5+
self.val = val
6+
self.next = next
7+
8+
class Solution:
9+
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
10+
if (not list1) or (list2 and list1.val > list2.val):
11+
list1, list2 = list2, list1
12+
if list1:
13+
list1.next = self.mergeTwoLists(list1.next, list2)
14+
15+
return list1
16+

0 commit comments

Comments
 (0)