From 0da372515a727512a6d32277a578112874d0fd52 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 6 Feb 2026 04:54:15 -0600 Subject: [PATCH] adding algo --- .../ex_67_same_tree.py | 19 +++++++++++ .../ex_68_symmetric_binary_tree.py | 33 +++++++++++++++++++ .../ex_67_same_tree.ts | 11 +++++++ .../ex_68_symmetric_binary_tree.ts | 24 ++++++++++++++ .../test_67_same_tree_round_22.py | 20 +++++++++++ .../test_68_symmetric_binary_tree_round_22.py | 15 +++++++++ 6 files changed, 122 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_67_same_tree.py create mode 100644 src/my_project/interviews/top_150_questions_round_22/ex_68_symmetric_binary_tree.py create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_67_same_tree.ts create mode 100644 src/my_project/interviews_typescript/top_150_questions_round_1/ex_68_symmetric_binary_tree.ts create mode 100644 tests/test_150_questions_round_22/test_67_same_tree_round_22.py create mode 100644 tests/test_150_questions_round_22/test_68_symmetric_binary_tree_round_22.py diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_67_same_tree.py b/src/my_project/interviews/top_150_questions_round_22/ex_67_same_tree.py new file mode 100644 index 00000000..60b4dfde --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_67_same_tree.py @@ -0,0 +1,19 @@ +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 isSameTree(self, p: TreeNode, q: TreeNode) -> bool: + + if p and q: + return p.val == q.val \ + and self.isSameTree(p.left, q.left) \ + and self.isSameTree(p.right, q.right) + else: + return p is q + \ No newline at end of file diff --git a/src/my_project/interviews/top_150_questions_round_22/ex_68_symmetric_binary_tree.py b/src/my_project/interviews/top_150_questions_round_22/ex_68_symmetric_binary_tree.py new file mode 100644 index 00000000..3bd90248 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_22/ex_68_symmetric_binary_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/src/my_project/interviews_typescript/top_150_questions_round_1/ex_67_same_tree.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_67_same_tree.ts new file mode 100644 index 00000000..7279aa9e --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_67_same_tree.ts @@ -0,0 +1,11 @@ +import { TreeNode } from './TreeNode'; + +function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean { + if (p && q) { + return p.val === q.val + && isSameTree(p.left, q.left) + && isSameTree(p.right, q.right); + } else { + return p === q; + } +} diff --git a/src/my_project/interviews_typescript/top_150_questions_round_1/ex_68_symmetric_binary_tree.ts b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_68_symmetric_binary_tree.ts new file mode 100644 index 00000000..cb41f391 --- /dev/null +++ b/src/my_project/interviews_typescript/top_150_questions_round_1/ex_68_symmetric_binary_tree.ts @@ -0,0 +1,24 @@ +import { TreeNode } from './TreeNode'; + +function isSymmetric(root: TreeNode | null): boolean { + if (!root) return true; + return checkMirror(root, root); +} + +function checkMirror(root1: TreeNode | null, root2: TreeNode | null): boolean { + if (root1 === null && root2 === null) { + return true; + } else if (root1 !== null && root2 !== null) { + if (root1.val !== root2.val) { + return false; + } else { + return checkMirror(root1.left, root2.right) + && checkMirror(root1.right, root2.left); + } + } else { + return false; + } +} + + + diff --git a/tests/test_150_questions_round_22/test_67_same_tree_round_22.py b/tests/test_150_questions_round_22/test_67_same_tree_round_22.py new file mode 100644 index 00000000..77e30a80 --- /dev/null +++ b/tests/test_150_questions_round_22/test_67_same_tree_round_22.py @@ -0,0 +1,20 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_67_same_tree import Solution, TreeNode + +class SameTreeTestCase(unittest.TestCase): + + def test_is_same_tree(self): + solution = Solution() + tree1 = TreeNode(1, TreeNode(2), TreeNode(3)) + tree2 = TreeNode(1, TreeNode(2), TreeNode(3)) + output = solution.isSameTree(p=tree1, q=tree2) + return self.assertTrue(output) + + def test_is_no_same_tree(self): + solution = Solution() + tree1 = TreeNode(1, TreeNode(2), TreeNode(3)) + tree2 = TreeNode(1, TreeNode(3), TreeNode(2)) + output = solution.isSameTree(p=tree1, q=tree2) + return self.assertFalse(output) + \ No newline at end of file diff --git a/tests/test_150_questions_round_22/test_68_symmetric_binary_tree_round_22.py b/tests/test_150_questions_round_22/test_68_symmetric_binary_tree_round_22.py new file mode 100644 index 00000000..2387cba3 --- /dev/null +++ b/tests/test_150_questions_round_22/test_68_symmetric_binary_tree_round_22.py @@ -0,0 +1,15 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_22\ +.ex_68_symmetric_binary_tree import Solution, TreeNode + +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