From b5d560939364e289dd3334a42b2bfa879982c454 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 10 Oct 2025 04:27:41 -0600 Subject: [PATCH] adding algo --- .../maximum_depth_tree.py | 16 ++++++++++++++++ .../test_maximum_depth_tree_round_20.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_20/maximum_depth_tree.py create mode 100644 tests/test_150_questions_round_20/test_maximum_depth_tree_round_20.py diff --git a/src/my_project/interviews/top_150_questions_round_20/maximum_depth_tree.py b/src/my_project/interviews/top_150_questions_round_20/maximum_depth_tree.py new file mode 100644 index 00000000..5d269794 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_20/maximum_depth_tree.py @@ -0,0 +1,16 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +class TreeNode: + def __init__(self, val=0, left=None, right=None): + self.val = val + self.left = left + self.right = right + +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + + if not root: + return 0 + else: + return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 \ No newline at end of file diff --git a/tests/test_150_questions_round_20/test_maximum_depth_tree_round_20.py b/tests/test_150_questions_round_20/test_maximum_depth_tree_round_20.py new file mode 100644 index 00000000..d9903b57 --- /dev/null +++ b/tests/test_150_questions_round_20/test_maximum_depth_tree_round_20.py @@ -0,0 +1,19 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_20\ +.maximum_depth_tree import Solution, TreeNode + +class MaxDepthTreeTestCase(unittest.TestCase): + + def test_max_depth_null(self): + solution = Solution() + tree = None + output = solution.maxDepth(root=tree) + target = 0 + self.assertEqual(output, target) + + def test_max_depth(self): + solution = Solution() + tree = TreeNode(1) + output = solution.maxDepth(root=tree) + target = 1 + self.assertEqual(output, target) \ No newline at end of file