From 87b4c1ccf4c534bd9d166975bc23044851d310e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=92=D0=B0=D1=81?= =?UTF-8?q?=D0=B8=D0=BB=D1=8C=D0=B5=D0=B2?= Date: Sat, 21 Feb 2026 17:40:08 +0300 Subject: [PATCH 1/2] feat: [LeetCode #104] Maximum Depth of Binary Tree MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Тип: Binary Tree Сложность: easy Временная сложность: O(n) Пространственная сложность: O(1) - Ссылка: https://leetcode.com/problems/maximum-depth-of-binary-tree/ --- src/binary_tree/__init__.py | 0 src/binary_tree/dfs/__init__.py | 0 .../maximum_depth_of_binary_tree/__init__.py | 0 .../maximum_depth_of_binary_tree/solution.py | 9 ++++++ structures/__init__.py | 3 ++ structures/tree.py | 5 ++++ test_utils/__init__.py | 3 ++ test_utils/tree_builder.py | 29 +++++++++++++++++++ tests/test_maximum_depth_of_binary_tree.py | 16 ++++++++++ 9 files changed, 65 insertions(+) create mode 100644 src/binary_tree/__init__.py create mode 100644 src/binary_tree/dfs/__init__.py create mode 100644 src/binary_tree/dfs/maximum_depth_of_binary_tree/__init__.py create mode 100644 src/binary_tree/dfs/maximum_depth_of_binary_tree/solution.py create mode 100644 structures/__init__.py create mode 100644 structures/tree.py create mode 100644 test_utils/__init__.py create mode 100644 test_utils/tree_builder.py create mode 100644 tests/test_maximum_depth_of_binary_tree.py diff --git a/src/binary_tree/__init__.py b/src/binary_tree/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/binary_tree/dfs/__init__.py b/src/binary_tree/dfs/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/binary_tree/dfs/maximum_depth_of_binary_tree/__init__.py b/src/binary_tree/dfs/maximum_depth_of_binary_tree/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/binary_tree/dfs/maximum_depth_of_binary_tree/solution.py b/src/binary_tree/dfs/maximum_depth_of_binary_tree/solution.py new file mode 100644 index 0000000..2c3548f --- /dev/null +++ b/src/binary_tree/dfs/maximum_depth_of_binary_tree/solution.py @@ -0,0 +1,9 @@ +from typing import Optional +from structures import TreeNode + + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) diff --git a/structures/__init__.py b/structures/__init__.py new file mode 100644 index 0000000..63206ca --- /dev/null +++ b/structures/__init__.py @@ -0,0 +1,3 @@ +from .tree import TreeNode + +__all__ = ["TreeNode"] diff --git a/structures/tree.py b/structures/tree.py new file mode 100644 index 0000000..6a976a2 --- /dev/null +++ b/structures/tree.py @@ -0,0 +1,5 @@ +class TreeNode: + def __init__(self, val: int = 0, left: int | None = None, right: int | None = None): + self.val = val + self.left = left + self.right = right diff --git a/test_utils/__init__.py b/test_utils/__init__.py new file mode 100644 index 0000000..b6808f5 --- /dev/null +++ b/test_utils/__init__.py @@ -0,0 +1,3 @@ +from .tree_builder import TreeBuilder + +__all__ = ["TreeBuilder"] diff --git a/test_utils/tree_builder.py b/test_utils/tree_builder.py new file mode 100644 index 0000000..50c1ad9 --- /dev/null +++ b/test_utils/tree_builder.py @@ -0,0 +1,29 @@ +from collections import deque +from typing import Optional +from structures import TreeNode + + +class TreeBuilder: + @staticmethod + def from_list(values: list[Optional[int]]) -> Optional[TreeNode]: + if not values or values[0] is None: + return None + + root = TreeNode(values[0]) + queue = deque([root]) + i = 1 + + while queue and i < len(values): + node = queue.popleft() + + if i < len(values) and values[i] is not None: + node.left = TreeNode(values[i]) + queue.append(node.left) + i += 1 + + if i < len(values) and values[i] is not None: + node.right = TreeNode(values[i]) + queue.append(node.right) + i += 1 + + return root diff --git a/tests/test_maximum_depth_of_binary_tree.py b/tests/test_maximum_depth_of_binary_tree.py new file mode 100644 index 0000000..ae98a60 --- /dev/null +++ b/tests/test_maximum_depth_of_binary_tree.py @@ -0,0 +1,16 @@ +import pytest +from src.binary_tree.dfs.maximum_depth_of_binary_tree.solution import Solution +from test_utils import TreeBuilder + + +@pytest.mark.parametrize( + "root, expected", + [ + ([3, 9, 20, None, None, 15, 7], 3), + ([1, None, 2], 2), + ], +) +def test_max_depth(root, expected): + root = TreeBuilder.from_list(root) + solution = Solution() + assert solution.maxDepth(root) == expected From 9ef49489396f227d15bfab9dce478852aac061f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=92=D0=B0=D1=81?= =?UTF-8?q?=D0=B8=D0=BB=D1=8C=D0=B5=D0=B2?= Date: Sat, 21 Feb 2026 17:42:57 +0300 Subject: [PATCH 2/2] fix: black reformatting --- src/stack/decode_string/solution.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stack/decode_string/solution.py b/src/stack/decode_string/solution.py index 6ac71ff..cc54480 100644 --- a/src/stack/decode_string/solution.py +++ b/src/stack/decode_string/solution.py @@ -6,10 +6,10 @@ def decodeString(self, s: str) -> str: for char in s: if char.isdigit(): number.append(char) - elif char == '[': + elif char == "[": stack.append("".join(number)) number = [] - elif char == ']': + elif char == "]": alphas = [] while stack[-1].isalpha(): alphas.append(stack.pop())