From 8f91b8585fee97e025e70c0da729485c132d4ec8 Mon Sep 17 00:00:00 2001 From: ivan Date: Mon, 13 Oct 2025 04:31:43 -0600 Subject: [PATCH] adding algo --- .../symmetric_tree.py | 32 +++++++++++++++++++ .../test_symmetric_tree_round_20.py | 15 +++++++++ 2 files changed, 47 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_20/symmetric_tree.py create mode 100644 tests/test_150_questions_round_20/test_symmetric_tree_round_20.py diff --git a/src/my_project/interviews/top_150_questions_round_20/symmetric_tree.py b/src/my_project/interviews/top_150_questions_round_20/symmetric_tree.py new file mode 100644 index 00000000..5343257d --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_20/symmetric_tree.py @@ -0,0 +1,32 @@ +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 isSymmetric(self, root: TreeNode) -> bool: + return self.check_mirror(root1=root, root2=root) + + def check_mirror(self, root1: TreeNode, root2: TreeNode): + + if root1 is None and root2 is None: + return True + elif root1 is not None and root2 is not None: + try: + root1.val + root2.val + except: + return False + + if root1.val != root2.val: + return False + else: + return self.check_mirror(root1.left, root2.right) \ + and self.check_mirror(root1.right, root2.left) + else: + return False diff --git a/tests/test_150_questions_round_20/test_symmetric_tree_round_20.py b/tests/test_150_questions_round_20/test_symmetric_tree_round_20.py new file mode 100644 index 00000000..57a392da --- /dev/null +++ b/tests/test_150_questions_round_20/test_symmetric_tree_round_20.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_20\ +.symmetric_tree import TreeNode, Solution + +class SymmetricTreeTestCase(unittest.TestCase): + + def test_is_symmetric(self): + solution = Solution() + output = solution.isSymmetric(TreeNode(1, TreeNode(7), TreeNode(7))) + self.assertTrue(output) + + def test_is_no_symmetric(self): + solution = Solution() + output = solution.isSymmetric(TreeNode(1, TreeNode(2), TreeNode(3))) + self.assertFalse(output) \ No newline at end of file