From e1c9d636fb806b0d0c0b77cca9c63ccacb6cec6f Mon Sep 17 00:00:00 2001 From: ivan Date: Thu, 22 May 2025 04:27:19 -0600 Subject: [PATCH] adding algo --- .../maximum_depth.py | 16 +++++++++++++++ .../test_maximum_depth_round_16.py | 20 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_16/maximum_depth.py create mode 100644 tests/test_150_questions_round_16/test_maximum_depth_round_16.py diff --git a/src/my_project/interviews/top_150_questions_round_16/maximum_depth.py b/src/my_project/interviews/top_150_questions_round_16/maximum_depth.py new file mode 100644 index 00000000..db949c51 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_16/maximum_depth.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 diff --git a/tests/test_150_questions_round_16/test_maximum_depth_round_16.py b/tests/test_150_questions_round_16/test_maximum_depth_round_16.py new file mode 100644 index 00000000..fb6e5957 --- /dev/null +++ b/tests/test_150_questions_round_16/test_maximum_depth_round_16.py @@ -0,0 +1,20 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_16\ +.maximum_depth 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