-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0100_same_tree.py
More file actions
64 lines (56 loc) · 1.87 KB
/
0100_same_tree.py
File metadata and controls
64 lines (56 loc) · 1.87 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
# Definition for a binary tree node.
import collections
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class InOrderSolution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
def preorder(root):
if root is None:
return []
stack = [root]
seq = []
while stack:
node = stack.pop()
if node is None:
seq.append(None)
continue
seq.append(node.val)
stack.append(node.right)
stack.append(node.left)
# seq.append(node.val)
# if not node.left and not node.right:
# continue
# if node.left is None:
# seq.append(None)
# else:
# stack.append(node.left)
# if node.right is not None:
# stack.append(node.right)
return seq
p_seq = preorder(p)
q_seq = preorder(q)
return p_seq == q_seq
class IntuitiveIterativeSolution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
# intuitive comparison
def check(p, q):
if not p and not q:
return True
if not p or not q:
return False
if p.val != q.val:
return False
return True
queue = collections.deque([(p, q)])
while queue:
p, q = queue.popleft()
# p=[], q is not None or p is not None and q=[] will be checked
if not check(p, q):
return False
if p is not None:
queue.append((p.left, q.left))
queue.append((p.right, q.right))
return True