From d4fa74064052f2af97b71d5e35bce1f7e6456123 Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 26 Nov 2024 04:37:25 -0600 Subject: [PATCH] adding same tree algo --- .../top_150_questions_round_11/same_tree.py | 18 ++++++++++++++++++ .../test_same_tree_round_11.py | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/my_project/interviews/top_150_questions_round_11/same_tree.py create mode 100644 tests/test_150_questions_round_11/test_same_tree_round_11.py diff --git a/src/my_project/interviews/top_150_questions_round_11/same_tree.py b/src/my_project/interviews/top_150_questions_round_11/same_tree.py new file mode 100644 index 00000000..c71d6cee --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_11/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_11/test_same_tree_round_11.py b/tests/test_150_questions_round_11/test_same_tree_round_11.py new file mode 100644 index 00000000..498eaed0 --- /dev/null +++ b/tests/test_150_questions_round_11/test_same_tree_round_11.py @@ -0,0 +1,19 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_11\ +.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) \ No newline at end of file