Flip Equivalent Binary Trees LeetCode solution in Python
1class Solution:2def flipEquiv(self, root1: TreeNode, root2: TreeNode) -> bool:3queue = deque([(root1, root2)])4while queue:5node1, node2 = queue.pop()6if (not node1) and (not node2):7continue8elif (not node1) or (not node2) or (node1.val != node2.val):9return False10L1, R1, L2, R2 = node1.left, node1.right, node2.left, node2.right11if (L1 and L2 and L1.val == L2.val) or (R1 and R2 and R1.val == R2.val):12queue.append((L1, L2))13queue.append((R1, R2))14else:15queue.append((L1, R2))16queue.append((L2, R1))17return True