-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathdusunax.py
37 lines (30 loc) · 901 Bytes
/
dusunax.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
37
'''
# 100. Same Tree
## Base case
- if both nodes are None, return True
- if one of the nodes is None, return False
- if the values of the nodes are different, return False
## Recursive case
- check if the left subtrees are the same
- check if the right subtrees are the same
## Time and Space Complexity
```
TC: O(n)
SC: O(n)
```
#### TC is O(n):
- visiting each node once, balanced or skewed, it's O(n)
#### SC is O(n):
- for h is tree's height.
- in best case(balanced): h is logN, so SC is O(logN)
- in worst case(skewed): h is N, so SC is O(N)
'''
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)