LeetCode 226. 翻转二叉树

leetcode.cn/problems/in...

programmercarl.com/0226.%E7%BF...

给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。

示例 1:

ini 复制代码
输入: root = [4,2,7,1,3,6,9]
输出: [4,7,2,9,6,3,1]

示例 2:

ini 复制代码
输入: root = [2,1,3]
输出: [2,3,1]

示例 3:

ini 复制代码
输入: root = []
输出: []

提示:

  • 树中节点数目范围在 [0, 100]
  • -100 <= Node.val <= 100

如果要从整个树来看,翻转还真的挺复杂,整个树以中间分割线进行翻转,如图:

可以发现想要翻转它,其实就把每一个节点的左右孩子交换一下就可以了。

遍历的过程中去翻转每一个节点的左右孩子就可以达到整体翻转的效果。

注意只要把每一个节点的左右孩子翻转一下,就可以达到整体翻转的效果

这道题目使用前序遍历和后序遍历都可以,唯独中序遍历不方便,因为中序遍历会把某些节点的左右孩子翻转了两次!建议拿纸画一画,就理解了

那么层序遍历可以不可以呢?依然可以的!只要把每一个节点的左右孩子翻转一下的遍历方式都是可以的!

递归法

我们下文以前序遍历为例,通过动画来看一下翻转的过程:

我们来看一下递归三部曲:

  1. 确定递归函数的参数和返回值

参数就是要传入节点的指针,不需要其他参数了,通常此时定下来主要参数,如果在写递归的逻辑中发现还需要其他参数的时候,随时补充。

返回值的话其实也不需要,但是题目中给出的要返回root节点的指针,可以直接使用题目定义好的函数,所以就函数的返回类型为TreeNode*

java 复制代码
TreeNode* invertTree(TreeNode* root)
  1. 确定终止条件

当前节点为空的时候,就返回

java 复制代码
if (root == NULL) return root;
  1. 确定单层递归的逻辑

因为是前序遍历,所以先进行交换左右孩子节点,然后反转左子树,反转右子树。

java 复制代码
swap(root->left, root->right);
invertTree(root->left);
invertTree(root->right);

基于这递归三步法,代码基本写完,C++代码如下:

java 复制代码
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (root == NULL) return root;
        swap(root->left, root->right);  // 中
        invertTree(root->left);         // 左
        invertTree(root->right);        // 右
        return root;
    }
};

kotlin 代码如下:

kotlin 复制代码
class Solution {
    fun invertTree(root: TreeNode?): TreeNode? {
        if (root == null) return root
        val temp = root.left
        root.left = root.right
        root.right = temp
        invertTree(root.left)
        invertTree(root.right)
        return root
    }
}

深度优先遍历

基于前序遍历,kotlin 代码:

kotlin 复制代码
class Solution {
    fun invertTree(root: TreeNode?): TreeNode? {
        val stack = Stack<TreeNode>()
        stack.push(root)
        while (stack.isNotEmpty()) {
            val top = stack.pop()
            if (top != null) {
                val temp = top.left
                top.left = top.right
                top.right = temp
                if (top.right != null) {
                    stack.push(top.right)
                }
                if (top.left != null) {
                    stack.push(top.left)
                }
            }
        }
        return root
    }
}

广度优先遍历

也就是层序遍历,层数遍历也是可以翻转这棵树的,因为层序遍历也可以把每个节点的左右孩子都翻转一遍,代码如下:

java 复制代码
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                swap(node->left, node->right); // 节点处理
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return root;
    }
};

kotlin 代码:

kotlin 复制代码
class Solution {
    fun invertTree(root: TreeNode?): TreeNode? {
        if (root == null) return root
        val queue = Stack<TreeNode>()
        queue.push(root)
        while (queue.isNotEmpty()) {
            val size = queue.size
            for (i in 0..<size) {
                val cur = queue.pop()
                val temp = cur.left
                cur.left = cur.right
                cur.right = temp
                if (cur.left != null) {
                    queue.push(cur.left)
                }
                if (cur.right != null) {
                    queue.push(cur.right)
                }
            }
        }
        return root
    }
}
相关推荐
啦啦啦啦啦zzzz5 分钟前
贪心算法和动态规划
c++·算法·贪心算法·动态规划
happyprince15 分钟前
篇3:bitsandbytes-深刻观-哲学与升华
人工智能·算法
应用市场23 分钟前
红绿灯背后的算法:从 Webster 配时到 Max-Pressure 与强化学习
算法
木井巳33 分钟前
【DFS解决floodfill算法】岛屿的最大面积
java·算法·leetcode·深度优先
June`2 小时前
常量内存和只读缓存
c++·人工智能·算法·cuda
一条大祥脚2 小时前
ABC468 扫描线|贡献法|二阶差分|线段树优化DP
数据结构·算法
happyprince2 小时前
篇2-bitsandbytes-具体观-算法与实现剖析
算法
普通攻击往后拉12 小时前
Leetcode 206. 反转链表
算法·leetcode·链表
@syh.13 小时前
【贪心】矩阵消除游戏
算法·游戏·矩阵
可编程芯片开发13 小时前
基于零极点配置的PID控制系统simulink建模与仿真
算法