- 03. 数组中重复的数字
 - 04. 二维数组中的查找
 - 05.替换空格
 - 06.从尾到头打印链表
 - 07.重建二叉树
 - 09.用两个栈实现队列
 - 10-1.斐波那契数列
 - 10-2.青蛙跳台阶问题
 - 剑指 offer 11.旋转数组的最小数字
 - 剑指offer 12.矩阵中的路径
 - 剑指offer 13.机器人的运动范围
 - 剑指offer 14-1.剪绳子
 - 剑指offer 14-2.剪绳子2
 - 剑指offer 15.二进制中1的个数
 - 剑指offer 16.数值的整数次方
 - 剑指offer 17.打印从1到最大的n位数
 - 剑指offer 18.删除链表的节点
 - 剑指offer 19.正则表达式匹配
 - 剑指offer 20.表示数值的字符串
 - 剑指offer 21.调整数组顺序使奇数位于偶数前面
 - 剑指offer 22.链表中倒数第k个节点
 - 剑指offer 24.反转链表
 - 剑指offer 25.合并两个排序的链表
 - 剑指offer 26.树的子结构
 - 剑指offer 27.二叉树的镜像
 - 剑指offer 28.对称的二叉树
 - 剑指offer 29.顺时针矩阵
 - 剑指offer 30.包含min函数的栈
 - 剑指offer 31.栈的压入、弹出序列
 - 剑指offer 32-1.从上到下打印二叉树
 - 剑指offer 32-2.从上到下打印二叉树
 - 剑指offer 32-3.从上到下打印二叉树
 - 剑指offer 33.二叉搜索树的后序遍历序列
 - 剑指offer 34.二叉树中和为某一值的路径
 - 剑指offer 35.复杂链表的复制
 - 剑指offer 36.二叉搜索树与双向链表
 - 剑指offer 37.序列化二叉树
 - 剑指offer 38.字符串的排列
 - 剑指 offer 39. 数组中出现次数超过一半的数字
 - 剑指offer 40. 最小的k个数
 - 剑指offer 41.数据流中的中位数
 - 剑指offer 42. 连续子数组的最大和
 - 剑指offer 43. 1~n整数中1出现的次数
 - 剑指offer 44. 数字序列中某一位的数字
 - 剑指offer 45. 把数组排成最小的数
 - 剑指offer 46.把数字翻译成字符串
 - 剑指offer 47.礼物的最大值
 - 剑指offer 48.最长不含重复字符的字符子串
 - 剑指offer 49.丑数
 - 剑指offer 50.第一个只出现一次的字符
 - 剑指offer 51. 数组中的逆序对
 - 剑指offer 52.两个链表的第一个公共节点
 - 剑指offer 53-1. 在排序数组中查找数字
 - 剑指offer 53-2. 0~n-1中缺失的数字
 - 剑指offer 54. 二叉搜索树的第k大节点
 - 剑指offer 55-1. 二叉树的深度
 - 剑指offer 55-2. 平衡二叉树
 - 剑指offer 56-1. 数组中数字出现的次数
 - 剑指offer 56-2. 数字中出现的次数
 - 剑指offer 57-1. 和为s的两个数字
 - 剑指offer 57-2. 和为s的连续正数序列
 - 剑指offer 58-1. 翻转单词顺序
 - 剑指offer 58-2.左旋转字符串
 - 剑指offer 59-1. 滑动窗口的最大值
 - 剑指offer 59-2. 队列的最大值
 - 剑指offer 60. n个骰子的点数
 - 剑指offer 61. 扑克牌中的顺子
 - 剑指offer 62. 圆圈中最后剩下的数字
 - 剑指offer 63. 股票的最大利润
 - 剑指offer 64. 求1+2+…+n
 - 剑指offer 65. 不用加减乘除做加法
 - 剑指offer 66. 构建乘积数组
 - 剑指offer 67. 把字符串转成整数
 - 剑指offer 68-1. 二叉搜索树的最近公共祖先
 - 剑指offer 68-2. 二叉树的最近公共祖先
 
剑指offer 68-2. 二叉树的最近公共祖先
题目描述
请完成一个函数,输入一个二叉树,该函数输出它的镜像。
例如输入:
     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
题解
(BFS,前序遍历) O(n)
翻转二叉树就是翻转二叉树的每个节点的左右孩子,把握住这一点,首先我们需要遍历二叉树,遍历二叉树的方法有很多,比如:前序遍历、后序遍历、层序遍历,但只有中序遍历不可以。
为什么中序边里不可以? 中序遍历会导致某些节点的左右孩子被翻转两次,而某些节点却一次都没有翻转,这是和中序遍历的过程有关系的,当我们遍历的当前节点是根节点的时候,首先翻转根节点的左右孩子,下一步要将根节点的右孩子的左边一条链都加入到栈中,而此时的右孩子恰恰是原树的左孩子,所以原树的左子树又会被翻转一次,而原树的右孩子却一次都没有被翻转,因此不能用中序遍历来翻转二叉树。
时间复杂度
O(n)
空间复杂度
O(logn),最坏情况下二叉树呈链状,空间复杂度为 O(n)。
C++代码
/**
 * 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) return nullptr;
        mirrorTree(root->left);
        mirrorTree(root->right);
        swap(root->left, root->right);
        return root;
    }
};Java 代码
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        mirrorTree(root.left);
        mirrorTree(root.right);
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        return root;
    }
}Python 代码
# 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 None
        self.mirrorTree(root.left)
        self.mirrorTree(root.right)
        root.left, root.right = root.right, root.left
        return root本文由读者提供,Github地址:https://github.com/tonngw
点击面试手册,获取本站面试手册PDF完整版