From 5e1c566f8b50a7cfe5841de6d2696ca89fd771fc Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 5 Sep 2025 04:23:19 -0600 Subject: [PATCH] adding algo --- .../top_150_questions_round_19/same_tree.py | 18 +++++++++++++++++ .../test_same_tree_round_19.py | 20 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_19/same_tree.py create mode 100644 tests/test_150_questions_round_19/test_same_tree_round_19.py diff --git a/src/my_project/interviews/top_150_questions_round_19/same_tree.py b/src/my_project/interviews/top_150_questions_round_19/same_tree.py new file mode 100644 index 00000000..c8b1a9e3 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_19/same_tree.py @@ -0,0 +1,18 @@ +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 diff --git a/tests/test_150_questions_round_19/test_same_tree_round_19.py b/tests/test_150_questions_round_19/test_same_tree_round_19.py new file mode 100644 index 00000000..5b4298e1 --- /dev/null +++ b/tests/test_150_questions_round_19/test_same_tree_round_19.py @@ -0,0 +1,20 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_19\ +.same_tree import TreeNode, Solution + +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) +