-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100__same_tree.py
36 lines (27 loc) · 1.05 KB
/
100__same_tree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import unittest
from typing import Optional
from lib.TreeNode import TreeNode, build_tree
class Solution(unittest.TestCase):
def generate_test_options(self):
options = [
(True, [1, 2, 3], [1, 2, 3]),
(True, [], []),
(False, [1, 2, 1], [1, 1, 2]),
(False, [], [1]),
(True, [1], [1]),
(False, [1, 2, None], [1, None, 2])
]
for expected, left, right in options:
yield (expected, build_tree(left), build_tree(right))
def test(self) -> None:
for expected, left, right in self.generate_test_options():
with self.subTest():
self.assertEqual(expected, self.isSameTree(left, right))
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if p is None and q is None:
return True
if p is None or q is None:
return False
if p.val != q.val:
return False
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)