forked from zchenpy/DS_Leetcode_82-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0100. Same Tree.py
52 lines (46 loc) · 1.62 KB
/
0100. 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
https://leetcode.com/problems/same-tree/
#1. Recursion:
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p and not q:
return True
if not p and q:
return False
if p and 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)
#Time complexity: O(min(n,m))
#Space complexity: O(min(n,m)) for extra stack space in recursion
#2. Queue
from collections import deque
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
queue = deque([(p,q),])
while queue:
(p,q)=queue.popleft()
if not p and not q:
continue
elif p and q and p.val==q.val:
queue.append((p.left,q.left))
queue.append((p.right,q.right))
else: return False
return True
#Time complexity: O(min(n,m))
#Space complexity: O(min(n,m))
#3.Stack
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
stack = [(p,q),]
while stack:
(check_p,check_q) = stack.pop()
if not check_p and not check_q:
continue
elif check_p and check_q and check_p.val == check_q.val:
stack.append((check_p.right,check_q.right))
stack.append((check_p.left,check_q.left))
else: return False
return True
#Time complexity: O(min(n,m))
#Space complexity: O(min(n,m))