当前位置: 首页 > news >正文

每日一题---翻转二叉树

题目:翻转二叉树

注意与对称二叉树区分 

题解:

解法一:递归

这道题比较简单,所以有许多思路,我先展示个人认为最容易理解的递归

1.先处理业务,再完成向下递归的操作

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode tmp = root.left; // 交换左右儿子
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left); // 翻转左子树
        invertTree(root.right); // 翻转右子树
        return root;
    }
}

2.使用临时变量存储递归后的节点的左右

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode left = invertTree(root.left); // 翻转左子树
        TreeNode right = invertTree(root.right); // 翻转右子树
        root.left = right; // 交换左右儿子
        root.right = left;
        return root;
    }
}

解法二:栈

这里借用Krahets的代码进行讲解

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if (root == null) return null;
        Stack<TreeNode> stack = new Stack<>() {{ add(root); }};
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            if (node.left != null) stack.add(node.left);
            if (node.right != null) stack.add(node.right);
            TreeNode tmp = node.left;
            node.left = node.right;
            node.right = tmp;
        }
        return root;
    }
}

图解:

root出栈 root.left,root.right入栈

进行出栈-交换-入栈

以此类推

 一次出栈两个并交换再入栈,直到为空...

那么以上就是全部题解了,欢迎大家补充更多解题思路,如有问题也欢迎大家指正!


文章转载自:
http://beano.hfstrb.cn
http://ankyloglossia.hfstrb.cn
http://aye.hfstrb.cn
http://carpogenic.hfstrb.cn
http://apagogic.hfstrb.cn
http://bedload.hfstrb.cn
http://atonic.hfstrb.cn
http://betty.hfstrb.cn
http://behtlehem.hfstrb.cn
http://assentient.hfstrb.cn
http://cassegrain.hfstrb.cn
http://amenorrhoea.hfstrb.cn
http://anthropologic.hfstrb.cn
http://bidon.hfstrb.cn
http://auxetic.hfstrb.cn
http://chicom.hfstrb.cn
http://beatnik.hfstrb.cn
http://basaltiform.hfstrb.cn
http://bagpiper.hfstrb.cn
http://antepenultimate.hfstrb.cn
http://anemophilous.hfstrb.cn
http://bedside.hfstrb.cn
http://castellany.hfstrb.cn
http://chiengmai.hfstrb.cn
http://bergall.hfstrb.cn
http://chalkboard.hfstrb.cn
http://charterer.hfstrb.cn
http://barometer.hfstrb.cn
http://chippewa.hfstrb.cn
http://chromophotograph.hfstrb.cn
http://www.dtcms.com/a/74016.html

相关文章:

  • 软件项目设计思维:从用户痛点到数字世界的优雅解构
  • 黑马商城完成随笔
  • 文心大模型4.5及X1重磅上线,真实测评
  • svg画图
  • MySQL复合查询
  • RocketMQ学习
  • 二次型 → 矩阵的正定性 → 特征值
  • 详解软件设计原则
  • python调用百度人脸识别接口
  • 第十一次CCF-CSP认证(含C++源码)
  • TensorFlow 基本原理与使用场景
  • 【SpringBatch】01简单入门
  • 简要分析NLMSG_DONE参数
  • Ubuntu下升级node.js从12.22到22.14
  • 商业智能BI分析中,汽车4S销售行业的返厂频次有什么分析价值?
  • 查看IP地址/Ping 命令
  • TX-LCN 框架
  • Couchbase Analytics 页面右侧的“Analytics Scopes, Links, Collections”等的解释
  • 在 ARM 嵌入式 Linux 下使用 C/C++ 实现 MQTT
  • HCIA-Access V2.5_14_3_1系统基本操作_管理操作用户
  • RxSwift 学习笔记第四篇之RxSwift在项目中的简单应用
  • Unix时间戳BKP备份寄存器RTC实时时钟
  • C# 不同框架如何调用framework 和 net core
  • 蓝桥杯 刷题统计
  • Vue:添加响应式数据
  • Mysql中创建表时的约束条件
  • 使用Dependency Walker和Beyond Compare快速排查dll动态库损坏或被篡改的问题
  • JavaScript如何做类型转换
  • Python文字识别OCR
  • 麒麟服务器操作系统QT系列软件工具手册