-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathday30_100.py
27 lines (20 loc) · 896 Bytes
/
day30_100.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
from typing import Optional
from helper import TreeNode, create_binary_tree
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if p is None and q is None:
return True
elif p and q and p.val == q.val:
return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)
else:
return False
if __name__ == "__main__":
p = create_binary_tree(nums=[1, 2, 3], index=0)
q = create_binary_tree(nums=[1, 2, 3], index=0)
assert Solution().isSameTree(p, q) is True
p = create_binary_tree(nums=[1, 2, None], index=0)
q = create_binary_tree(nums=[1, None, 2], index=0)
assert Solution().isSameTree(p, q) is False
p = create_binary_tree(nums=[1, 2, 1], index=0)
q = create_binary_tree(nums=[1, 1, 2], index=0)
assert Solution().isSameTree(p, q) is False