【剑指 Offer 27. 二叉树的镜像】
题目描述:
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/
- 请完成一个函数,输入一个二叉树,该函数输出它的镜像。
示例:
- 例如输入:
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]
- 示例2:
- 示例3:
输入:root = [2,1,3]
输出:[2,3,1]
输入:root = []
输出:[]
- 提示:
0 <= 节点个数 <= 1000
解析思路1:遍历节点、递归交换
-
递归遍历二叉树,交换每个节点的左 / 右子节点,可生成二叉树的镜像。
-
递归算法流程示意:
-
1、终止条件: 当节点 root 为空时(即不含叶节点),则返回 null ;
-
2、递推工作:
-
初始化节点 temp ,用于暂存 root 的左子节点;
-
开启递归 右子节点 mirrorTree(root.right) ,并将返回值作为 root 的 左子节点 。
-
开启递归 左子节点 mirrorTree(temp) ,并将返回值作为 root 的 右子节点 。
-
3、返回值: 返回当前节点 root ;
代码(python3)
遍历交换
# 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 # 声明一个变量temp ,存储二叉树的左节点
root.left = root.right
root.right = temp # 类似交换两个数,交换左右节点
self.mirrorTree(root.left)
self.mirrorTree(root.right)
return root
递归交换
# 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 # 声明一个变量temp ,存储二叉树的左节
root.left =self.mirrorTree(root.right) # 递归应用
root.right = self.mirrorTree(temp)
return root
代码(cpp)
遍历节点,递归交换
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(root == nullptr) return nullptr;
TreeNode* temp = root->left; // 声明暂存变量
root->left = mirrorTree(root->right); // 递归
root->right = mirrorTree(temp);
return root;
}
};
复杂度分析:
- 时间复杂度 O(N) : 其中 N 为二叉树的节点数量,建立二叉树镜像需要遍历树的所有节点,占用 O(N) 时间。
- 空间复杂度 O(N) : 最差情况下(当二叉树退化为链表),递归时系统需使用 O(N) 大小的栈空间。
解析思路2:使用辅助栈(或队列)
- 参考k神解析.
- 利用栈(或队列)遍历树的所有节点 node ,并交换每个 nodenode 的左 / 右子节点。
- 算法流程示意图:
参考图链接:https://leetcode-cn.com/problems/er-cha-shu-de-jing-xiang-lcof/solution/mian-shi-ti-27-er-cha-shu-de-jing-xiang-di-gui-fu-/
代码(python3)
# 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 # 当 root 为空时,直接返回 null ;
stack = [root] # 初始化 栈
while stack: # 循环交换,当栈 stack 为空时跳出循环;
node = stack.pop() # 出栈: 记为 node
if node.left: stack.append(node.left) # 添加子节点: 将 node 左和右子节点入栈;
if node.right: stack.append(node.right)
node.left, node.right = node.right, node.left # 交换: 交换 node 的左 / 右子节点。
return root # 返回值: 返回根节点 rootroot
代码(cpp)
class Solution {
public:
TreeNode* mirrorTree(TreeNode* root) {
if(root == nullptr) return nullptr; //特例处理: 当 root 为空时,直接返回 null ;
stack<TreeNode*> stack; // 初始化: 栈(或队列),本文用栈,并加入根节点 root 。
stack.push(root);
while (!stack.empty()) // 循环交换: 当栈 stack 为空时跳出;
{
TreeNode* node = stack.top(); // 出栈: 记为 node ;
stack.pop();
if (node->left != nullptr) stack.push(node->left); // 添加子节点: 将 node 左和右子节点入栈;
if (node->right != nullptr) stack.push(node->right);
TreeNode* tmp = node->left; // 交换 nodenode 的左 / 右子节点。
node->left = node->right;
node->right = tmp;
}
return root; // 返回根节点 root
}
};
复杂度分析:
- 时间复杂度 O(N) : 其中 N 为二叉树的节点数量,建立二叉树镜像需要遍历树的所有节点,占用 O(N) 时间。
- 空间复杂度 O(N) : 如下图所示,最差情况下,栈 stack 最多同时存储
(N+1)/2个节点,占用 O(N) 额外空间。