forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppxyn1.py
More file actions
21 lines (18 loc) · 659 Bytes
/
ppxyn1.py
File metadata and controls
21 lines (18 loc) · 659 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
# idea : DFS
# Inorder method loses structural information, which is not suitable for it.
# Time Complexity: O(p+q)
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p and not q:
return True
if not p or not q:
return False
if p.val != q.val:
return False
return (self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right))