Skip to content

Commit db5a4cf

Browse files
committed
add maximum depth
1 parent 8188ff5 commit db5a4cf

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

Diff for: src/linked_list/merge_two_sorted_list.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from typing import Optional
22

3+
34
class ListNode:
45
def __init__(self, val=0, next=None):
56
self.val = val
67
self.next = next
78

9+
810
class Solution:
9-
def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
11+
def mergeTwoLists(
12+
self, list1: Optional[ListNode], list2: Optional[ListNode]
13+
) -> Optional[ListNode]:
1014
dummy = ListNode()
1115
tail = dummy
1216

Diff for: src/linked_list/reorder_list.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from typing import Optional
22

3+
34
class ListNode:
45
def __init__(self, val=0, next=None):
56
self.val = val
67
self.next = next
78

9+
810
class Solution:
911
def reorderList(self, head: Optional[ListNode]) -> None:
1012
"""

Diff for: src/linked_list/reverse_linked_list.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from typing import Optional
22

3+
34
class ListNode:
45
def __init__(self, val=0, next=None):
56
self.val = val
67
self.next = next
78

9+
810
class Solution:
911
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
1012
prev, curr = None, head

Diff for: src/trees/max_depth_binary_tree.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import Optional
2+
from collections import deque
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def maxDepth_recursive(self, root: Optional[TreeNode]) -> int:
14+
if not root:
15+
return 0
16+
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
17+
18+
def maxDepth_bfs(self, root: Optional[TreeNode]) -> int:
19+
if not root:
20+
return 0
21+
22+
q = deque([root])
23+
level = 0
24+
while q:
25+
current_size = len(q)
26+
for _ in range(current_size):
27+
node = q.popleft()
28+
if node.left:
29+
q.append(node.left)
30+
if node.right:
31+
q.append(node.right)
32+
level += 1
33+
return level
34+
35+
def maxDepth_dfs(self, root: Optional[TreeNode]) -> int:
36+
stack = [[root, 1]]
37+
res = 0
38+
39+
while stack:
40+
node, depth = stack.pop()
41+
42+
if node:
43+
res = max(res, depth)
44+
stack.append([node.left, depth + 1])
45+
stack.append([node.right, depth + 1])
46+
47+
return res

Diff for: tests/test_time_based_kv_store.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ def test_time_based_kv_store():
2323
res = []
2424
for index in range(op_times):
2525
if tc["op"][index] == "set":
26-
tm.set(tc["value"][index][0], tc["value"][index][1], tc["value"][index][2])
26+
tm.set(
27+
tc["value"][index][0], tc["value"][index][1], tc["value"][index][2]
28+
)
2729
res.append("")
2830
else:
2931
res.append(tm.get(tc["value"][index][0], tc["value"][index][1]))

0 commit comments

Comments
 (0)