# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def flipEquiv(self, root1: TreeNode, root2: TreeNode) -> bool:
        if root1 is None or root2 is None:
            return root2 is None and root1 is None
        if root1.val != root2.val: return False
        left1 = root1.left.val if root1.left else None
        left2 = root2.left.val if root2.left else None
        if left1 == left2:
            return self.flipEquiv(root1.left, root2.left) and self.flipEquiv(root1.right, root2.right) 
        return self.flipEquiv(root1.left, root2.right) and self.flipEquiv(root1.right, root2.left)
        