951. 翻转等价二叉树
题目描述
解题思路
1、flipEquiv()判断两颗树是否相等
2、root1, root2都为空,看左右子树;
3、root1, root2有一个为空或值不等,不相等
代码实现
class Solution:
def flipEquiv(self, root1: TreeNode, root2: TreeNode) -> bool:
if not root1 and not root2:
return True
if not root1 or not root2 or root1.val != root2.val:
return False
ll = self.flipEquiv(root1.left, root2.left)
rr = self.flipEquiv(root1.right, root2.right)
lr = self.flipEquiv(root1.left, root2.right)
rl = self.flipEquiv(root1.right, root2.left)
return (ll and rr) or (lr and rl)