剑指 Offer 27. 二叉树的镜像
写在开头
每次遇见递归都会头疼一阵,所以为了搞清楚算法的原理和运行过程,我就用PyCharm进行调试,搞清楚每一步运行时变量的变化情况,特别是递归的反馈过程。这也是小编为什么坚持用PyCharm刷算法题的原因。当然,只针对一些过程较为复杂的算法。比如本题的递归过程有点绕,调试中就能一点点摸清楚,加深对算法的记忆。
题目描述
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
例如输入:
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
题目解析
二叉树镜像定义: 对于二叉树中任意节点 root
,设其左 / 右子节点分别为 left
, right
;则在二叉树的镜像中的对应 root
节点,其左 / 右子节点分别为 right
, left
。
方法1:递归法
- 根据二叉树镜像的定义,考虑递归遍历(dfs)二叉树,交换每个节点的左 / 右子节点,即可生成二叉树的镜像。
递归解析:
- 终止条件: 当节点
root
为空时(即越过叶节点),则返回 null ; - 递推工作:
- 初始化节点
tmp
,用于暂存root
的左子节点; - 开启递归 右子节点
mirrorTree(root.right)
,并将返回值作为root
的 左子节点 。 - 开启递归 左子节点
mirrorTree(tmp)
,并将返回值作为root
的 右子节点 。
返回值: 返回当前节点root
;
- 初始化节点
Q: 为何需要暂存
root
的左子节点?
A: 在递归右子节点 “root.left = mirrorTree(root.right);
” 执行完毕后,root.left
的值已经发生改变,此时递归左子节点mirrorTree(root.left)
则会出问题。
复杂度分析:
- 时间复杂度
O(N)
: 其中 NN 为二叉树的节点数量,建立二叉树镜像需要遍历树的所有节点,占用O(N)
时间。 - 空间复杂度
O(N)
: 最差情况下(当二叉树退化为链表),递归时系统需使用O(N)
大小的栈空间。
Python代码:
class Solution:
def mirrorTree(self, root):
if not root: return
tmp = root.left
root.left = self.mirrorTree(root.right)
root.right = self.mirrorTree(tmp)
return root
Python 利用平行赋值的写法(即 a, b = b, a
),可省略暂存操作。其原理是先将等号右侧打包成元组 (b,a)
,再序列地分给等号左侧的 a, b
序列。
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
if not root: return
root.left, root.right = self.mirrorTree(root.right), self.mirrorTree(root.left)
return root
方法二:辅助栈(或队列)
- 利用栈(或队列)遍历树的所有节点 node ,并交换每个 node 的左 / 右子节点。
算法流程:
- 特例处理: 当
root
为空时,直接返回null
; - 初始化: 栈(或队列),本文用栈,并加入根节点
root
。 - 循环交换: 当栈
stack
为空时跳出;- 出栈: 记为
node
; - 添加子节点: 将
node
左和右子节点入栈; - 交换: 交换
node
的左 / 右子节点。
- 出栈: 记为
- 返回值: 返回根节点
root
。
复杂度分析:
- 时间复杂度
O(N)
: 其中N
为二叉树的节点数量,建立二叉树镜像需要遍历树的所有节点,占用O(N)
时间。
空间复杂度O(N)
: 如下图所示,最差情况下,栈stack
最多同时存储(N + 1)/2
个节点,占用O(N)
额外空间。
Python代码
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
if not root: return
stack = [root]
while stack:
node = stack.pop()
if node.left: stack.append(node.left)
if node.right: stack.append(node.right)
node.left, node.right = node.right, node.left
return root
PyCharm调试
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object):
# 1. 递归
def mirrorTree_1(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if not root: return
# tmp = root.left
# root.left = self.mirrorTree(root.right)
# root.right = self.mirrorTree(tmp)
root.left, root.right = self.mirrorTree(root.right), self.mirrorTree_1(root.left)
return root
# 2. 辅助栈
def mirrorTree(self, root):
if not root: return
stack = [root]
while stack:
node = stack.pop()
if node.left: stack.append(node.left)
if node.right: stack.append(node.right)
node.left, node.right = node.right, node.left
return root
t1 = TreeNode(4)
t2 = TreeNode(2)
t3 = TreeNode(7)
t4 = TreeNode(1)
t5 = TreeNode(3)
t6 = TreeNode(6)
t7 = TreeNode(9)
t2.left = t4
t2.right = t5
t3.left = t6
t3.right = t7
t1.left = t2
t1.right = t3
solution = Solution()
a = solution.mirrorTree(t1)
本文转自:剑指 Offer 27 题目解析