码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 算法leetcode|剑指 Offer 27. 二叉树的镜像|226. 翻转二叉树(rust很强)



    文章目录

    • 剑指 Offer 27. 二叉树的镜像|226. 翻转二叉树:
    • 样例 1:
    • 限制:
    • 分析
    • 题解
      • rust
      • go
      • c++
      • java
      • python
    • 原题传送门:https://leetcode.cn/problems/er-cha-shu-de-jing-xiang-lcof/
    • 原题传送门:https://leetcode.cn/problems/invert-binary-tree/


    剑指 Offer 27. 二叉树的镜像|226. 翻转二叉树:

    请完成一个函数,输入一个二叉树,该函数输出它的镜像。

    例如输入:

         4
       /   \
      2     7
     / \   / \
    1   3 6   9
    
    • 1
    • 2
    • 3
    • 4
    • 5

    镜像输出:

         4
       /   \
      7     2
     / \   / \
    9   6 3   1
    
    • 1
    • 2
    • 3
    • 4
    • 5

    样例 1:

    输入:
    	root = [4,2,7,1,3,6,9]
    	
    输出:
    	[4,7,2,9,6,3,1]
    
    • 1
    • 2
    • 3
    • 4
    • 5

    限制:

    • 0 <= 节点个数 <= 1000

    分析

    • 面对这道算法题目,二当家的陷入了沉思。
    • 仔细思考就会发现啊,其实就是把每个节点的左右孩子交换。
    • 而树结构的遍历太适合用递归套娃大法了。

    题解

    rust

    // Definition for a binary tree node.
    // #[derive(Debug, PartialEq, Eq)]
    // pub struct TreeNode {
    //   pub val: i32,
    //   pub left: Option>>,
    //   pub right: Option>>,
    // }
    // 
    // impl TreeNode {
    //   #[inline]
    //   pub fn new(val: i32) -> Self {
    //     TreeNode {
    //       val,
    //       left: None,
    //       right: None
    //     }
    //   }
    // }
    use std::rc::Rc;
    use std::cell::RefCell;
    impl Solution {
        pub fn mirror_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Option<Rc<RefCell<TreeNode>>> {
            if root.is_some() {
                let mut mb = root.as_ref().unwrap().borrow_mut();
                let left = Solution::mirror_tree(mb.left.take());
                let right = Solution::mirror_tree(mb.right.take());
                mb.left = right;
                mb.right = left;
            }
            return root;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    go

    /**
     * Definition for a binary tree node.
     * type TreeNode struct {
     *     Val int
     *     Left *TreeNode
     *     Right *TreeNode
     * }
     */
    func mirrorTree(root *TreeNode) *TreeNode {
        if root != nil {
    		left := mirrorTree(root.Left)
    		right := mirrorTree(root.Right)
    		root.Left = right
    		root.Right = left
    	}
    	return root
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    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) {
                TreeNode *left = mirrorTree(root->left);
                TreeNode *right = mirrorTree(root->right);
                root->left = right;
                root->right = left;
            }
            return root;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    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) {
                TreeNode left = mirrorTree(root.left);
                TreeNode right = mirrorTree(root.right);
                root.left = right;
                root.right = left;
            }
            return root;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    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 root:
                left = self.mirrorTree(root.left)
                right = self.mirrorTree(root.right)
                root.left = right
                root.right = left
            return root
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    原题传送门:https://leetcode.cn/problems/er-cha-shu-de-jing-xiang-lcof/

    原题传送门:https://leetcode.cn/problems/invert-binary-tree/


    非常感谢你阅读本文~
    欢迎【点赞】【收藏】【评论】~
    放弃不难,但坚持一定很酷~
    希望我们大家都能每天进步一点点~
    本文由 二当家的白帽子:https://le-yi.blog.csdn.net/ 博客原创~


  • 相关阅读:
    SqlServer 存储过程使用整理
    win11安装更新错误0x800f081f怎么解决?
    Flask连接数据库
    Raspberry Pi 5 新平台 新芯片组
    深入了解- TCP拥塞状态机 tcp_fastretrans_alert
    Eth-Trunk负载分担不均怎么办,如何通过Hash算法实现负载分担?
    涛思 TDengine 表设计及SQL
    Redis概述
    数据结构与算法笔记:基础篇 - 初始动态规划:如何巧妙解决“双十一”购物时的凑单问题?
    Linux 之 Ubuntu 上 Vim 的安装、配置、常用命令的简单整理
  • 原文地址:https://blog.csdn.net/leyi520/article/details/125928249
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号