递归解法
class Solution: def mirrorTree(self, root: TreeNode) -> TreeNode: if not root:return root self.mirrorTree(root.left) self.mirrorTree(root.right) root.right,root.left=root.left,root.right return root
BFS解法
class Solution: def mirrorTree(self, root: TreeNode) -> TreeNode: #尝试BFS解法 if not root:return root queue=[root] while queue: next_queue=[] for node in queue: node.right,node.left=node.left,node.right if node.left:next_queue.append(node.left) if node.right:next_queue.append(node.right) queue=next_queue return root