From db128c0e3d675549058c631704dee78d2a6b2ae6 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 14 Mar 2025 04:25:47 -0600 Subject: [PATCH] adding same tree algo --- .../top_150_questions_round_14/same_tree.py | 18 ++++++++++++++++++ .../test_same_tree_round_14.py | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_14/same_tree.py create mode 100644 tests/test_150_questions_round_14/test_same_tree_round_14.py diff --git a/src/my_project/interviews/top_150_questions_round_14/same_tree.py b/src/my_project/interviews/top_150_questions_round_14/same_tree.py new file mode 100644 index 00000000..a208dfe7 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_14/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 \ No newline at end of file diff --git a/tests/test_150_questions_round_14/test_same_tree_round_14.py b/tests/test_150_questions_round_14/test_same_tree_round_14.py new file mode 100644 index 00000000..d2c0a2fd --- /dev/null +++ b/tests/test_150_questions_round_14/test_same_tree_round_14.py @@ -0,0 +1,19 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_14\ +.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