难度简单123
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
例如输入:
4
/ \
2 7
/ \ / \
1 3 6 9
镜像输出:
4
/ \
7 2
/ \ / \
9 6 3 1
示例 1:
输入:root = [4,2,7,1,3,6,9] 输出:[4,7,2,9,6,3,1]
限制:
0 <= 节点个数 <= 1000
注意:本题与主站 226 题相同:https://leetcode-cn.com/problems/invert-binary-tree/
方法1:递归
二叉树镜像定义: 对于二叉树中任意节点 rootroot ,设其左 / 右子节点分别为 left,right ;则在二叉树的镜像中的对应 root 节点,其左 / 右子节点分别为 right, left。
根据二叉树镜像的定义,考虑递归遍历(dfs)二叉树,交换每个节点的左 / 右子节点,即可生成二叉树的镜像。
(1)递归解析:
终止条件: 当节点 root 为空时(即越过叶节点),则返回 None ;
(2)递推工作:
1)初始化节点 temp ,用于暂存 root 的左子节点;
2)开启递归 右子节点 mirrorTree(root.right) ,并将返回值作为 root 的 左子节点 。
3)开启递归 左子节点 mirrorTree(temp) ,并将返回值作为 root 的 右子节点 。
4)返回值: 返回当前节点 root ;
Q: 为何需要暂存 rootroot 的左子节点?
A: 在递归右子节点 “root.left=mirrorTree(root.right);” 执行完毕后, root.left 的值已经发生改变,此时递归左子节点 mirrorTree(root.left) 则会出问题。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
if not root:
return
temp = root.left
root.left = self.mirrorTree(root.right)
root.right = self.mirrorTree(temp)
return root