[Leetcode] 951. Flip Equivalent Binary Trees

方法1

DFS

class Solution:
    def flipEquiv(self, root1: TreeNode, root2: TreeNode) -> bool:
        if not root1 and not root2:
            return True
        if not root1:
            return False
        if not root2:
            return False
        if root1.val != root2.val:
            return False
        if (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)):
            return True
        return False

你可能感兴趣的:(algorithm,leetcode,二叉树,dfs)