From 1715b26582b369a3fc4e18ac0463b1b195aa9efb Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 25 Nov 2024 04:27:35 -0600 Subject: [PATCH] adding max depth binary tree --- .../maximum_depth_binary_tree.py | 16 ++++++++++++++++ ...test_maximum_depth_binary_tree_round_11.py | 19 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_11/maximum_depth_binary_tree.py create mode 100644 tests/test_150_questions_round_11/test_maximum_depth_binary_tree_round_11.py diff --git a/src/my_project/interviews/top_150_questions_round_11/maximum_depth_binary_tree.py b/src/my_project/interviews/top_150_questions_round_11/maximum_depth_binary_tree.py new file mode 100644 index 00000000..5d269794 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_11/maximum_depth_binary_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_11/test_maximum_depth_binary_tree_round_11.py b/tests/test_150_questions_round_11/test_maximum_depth_binary_tree_round_11.py new file mode 100644 index 00000000..c487827d --- /dev/null +++ b/tests/test_150_questions_round_11/test_maximum_depth_binary_tree_round_11.py @@ -0,0 +1,19 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_11\ +.maximum_depth_binary_tree import TreeNode, Solution + +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)