From 5d91c1a4b91dfa49354e9702024a31939eaf088b Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 30 Nov 2024 04:28:38 -0600 Subject: [PATCH] adding count complete nodes --- .../count_complete_three_nodes.py | 17 ++++++++++++++++ ...est_count_complete_three_nodes_round_11.py | 20 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_11/count_complete_three_nodes.py create mode 100644 tests/test_150_questions_round_11/test_count_complete_three_nodes_round_11.py diff --git a/src/my_project/interviews/top_150_questions_round_11/count_complete_three_nodes.py b/src/my_project/interviews/top_150_questions_round_11/count_complete_three_nodes.py new file mode 100644 index 00000000..37674deb --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_11/count_complete_three_nodes.py @@ -0,0 +1,17 @@ +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 countNodes(self, root: Optional[TreeNode]) -> int: + + if not root: + return 0 + else: + return self.countNodes(root.left) + self.countNodes(root.right) + 1 \ No newline at end of file diff --git a/tests/test_150_questions_round_11/test_count_complete_three_nodes_round_11.py b/tests/test_150_questions_round_11/test_count_complete_three_nodes_round_11.py new file mode 100644 index 00000000..93c49c90 --- /dev/null +++ b/tests/test_150_questions_round_11/test_count_complete_three_nodes_round_11.py @@ -0,0 +1,20 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_11\ +.count_complete_three_nodes import TreeNode, Solution + +class CountNodesTestCase(unittest.TestCase): + + def test_count_none(self): + solution = Solution() + tree = None + output = solution.countNodes(root=tree) + self.assertEqual(0, output) + + + def test_count_non_empty_tree(self): + solution = Solution() + tree = TreeNode(1, TreeNode(2), TreeNode(3)) + output = solution.countNodes(root=tree) + self.assertEqual(3, output) + + \ No newline at end of file