diff --git a/src/my_project/interviews/top_150_questions_round_20/same_tree.py b/src/my_project/interviews/top_150_questions_round_20/same_tree.py new file mode 100644 index 00000000..ef0f5209 --- /dev/null +++ b/src/my_project/interviews/top_150_questions_round_20/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/tests/test_150_questions_round_20/test_same_tree_round_20.py b/tests/test_150_questions_round_20/test_same_tree_round_20.py new file mode 100644 index 00000000..640e4a02 --- /dev/null +++ b/tests/test_150_questions_round_20/test_same_tree_round_20.py @@ -0,0 +1,20 @@ +import unittest +from src.my_project.interviews.top_150_questions_round_20\ +.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