Leetcode 988 Solution

This article provides solution to leetcode question 988 (flip-equivalent-binary-trees)

https://leetcode.com/problems/flip-equivalent-binary-trees

Solution

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

class Solution(object):
    def flipEquiv(self, root1, root2):
        """
        :type root1: TreeNode
        :type root2: TreeNode
        :rtype: bool
        """
        if root1 is None and root2 is None:
            return True

        if root1 is None or root2 is None:
            return False

        if root1.val != root2.val:
            return False

        return self.flipEquiv(root1.left, root2.left) and self.flipEquiv(root1.right, root2.right) or \
            self.flipEquiv(root1.left, root2.right) and self.flipEquiv(root1.right, root2.left)