Flip Equivalent Binary Trees LeetCode solution in Python

1
class Solution:
2
def flipEquiv(self, root1: TreeNode, root2: TreeNode) -> bool:
3
queue = deque([(root1, root2)])
4
while queue:
5
node1, node2 = queue.pop()
6
if (not node1) and (not node2):
7
continue
8
elif (not node1) or (not node2) or (node1.val != node2.val):
9
return False
10
L1, R1, L2, R2 = node1.left, node1.right, node2.left, node2.right
11
if (L1 and L2 and L1.val == L2.val) or (R1 and R2 and R1.val == R2.val):
12
queue.append((L1, L2))
13
queue.append((R1, R2))
14
else:
15
queue.append((L1, R2))
16
queue.append((L2, R1))
17
return True

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0