-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0100-same-tree.py
More file actions
75 lines (56 loc) · 1.81 KB
/
0100-same-tree.py
File metadata and controls
75 lines (56 loc) · 1.81 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
65
66
67
68
69
70
71
72
73
74
75
from collections import deque
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
if not p:
if q:
return False
if not q: return True
if not q:
return False
search_p = deque()
search_p.append(p)
search_q = deque()
search_q.append(q)
while search_p and search_q:
check_p = search_p.popleft()
check_q = search_q.popleft()
if check_p.val != check_q.val:
return False
else:
# If both nodes have a left node, add them to queues
if check_p.left and check_q.left:
search_p.append(check_p.left)
search_q.append(check_q.left)
# If one node has a left node, but the other doesn't
# Trees are not the same
elif check_p.left != check_q.left:
return False
# Ditto right nodes
if check_p.right and check_q.right:
search_p.append(check_p.right)
search_q.append(check_q.right)
elif check_p.right != check_q.right:
return False
return True
def main():
sol = Solution()
p_head = TreeNode(1)
p_left = TreeNode(2)
p_right = TreeNode(3)
p_head.left = p_left
p_head.right = p_right
q_head = TreeNode(1)
q_left = TreeNode(2)
q_right = TreeNode(2)
q_head.left = q_left
q_head.right = q_right
null_head = TreeNode(None)
print(sol.isSameTree(null_head, null_head))
if __name__ == "__main__":
main()