From 21619c70e6a0efa1ccd899afeb22809f71298c0e Mon Sep 17 00:00:00 2001 From: ivan Date: Wed, 1 Jan 2025 04:34:32 -0600 Subject: [PATCH] adding same tree algo --- .../top_150_questions_round_12/same_tree.py | 18 +++++++++++++++++ .../test_same_tree_round_12.py | 20 +++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_12/same_tree.py create mode 100644 tests/test_150_questions_round_12/test_same_tree_round_12.py diff --git a/src/my_project/interviews/top_150_questions_round_12/same_tree.py b/src/my_project/interviews/top_150_questions_round_12/same_tree.py new file mode 100644 index 00000000..a208dfe7 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_12/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_12/test_same_tree_round_12.py b/tests/test_150_questions_round_12/test_same_tree_round_12.py new file mode 100644 index 00000000..00ed149a --- /dev/null +++ b/tests/test_150_questions_round_12/test_same_tree_round_12.py @@ -0,0 +1,20 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_12\ +.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)