diff --git a/src/my_project/interviews/top_150_questions_round_11/symmetric_tree.py b/src/my_project/interviews/top_150_questions_round_11/symmetric_tree.py new file mode 100644 index 00000000..8da93b95 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_11/symmetric_tree.py @@ -0,0 +1,33 @@ +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_11/test_symmetric_tree_round_11.py b/tests/test_150_questions_round_11/test_symmetric_tree_round_11.py new file mode 100644 index 00000000..fb2b058b --- /dev/null +++ b/tests/test_150_questions_round_11/test_symmetric_tree_round_11.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_11\ +.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