From 2a73651eb768f884416cb50a2135d71d79c6c96e Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 9 Sep 2025 04:29:48 -0600 Subject: [PATCH] adding algo --- .../count_complete_nodes.py | 17 +++++++++++++++++ .../test_count_complete_nodes_round_19.py | 18 ++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_19/count_complete_nodes.py create mode 100644 tests/test_150_questions_round_19/test_count_complete_nodes_round_19.py diff --git a/src/my_project/interviews/top_150_questions_round_19/count_complete_nodes.py b/src/my_project/interviews/top_150_questions_round_19/count_complete_nodes.py new file mode 100644 index 00000000..afe1edf7 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_19/count_complete_nodes.py @@ -0,0 +1,17 @@ +from typing import List, Union, Collection, Mapping, Optional +from abc import ABC, abstractmethod + +# Definition for a binary tree node. +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_19/test_count_complete_nodes_round_19.py b/tests/test_150_questions_round_19/test_count_complete_nodes_round_19.py new file mode 100644 index 00000000..dda8fc4e --- /dev/null +++ b/tests/test_150_questions_round_19/test_count_complete_nodes_round_19.py @@ -0,0 +1,18 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_19\ +.count_complete_nodes import Solution, TreeNode + +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)