码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode每日一题(1325. Delete Leaves With a Given Value)


    Given a binary tree root and an integer target, delete all the leaf nodes with value target.

    Note that once you delete a leaf node with value target, if its parent node becomes a leaf node and has the value target, it should also be deleted (you need to continue doing that until you cannot).

    Example 1:

    Input: root = [1,2,3,2,null,2,4], target = 2
    Output: [1,null,3,null,4]

    Explanation: Leaf nodes in green with value (target = 2) are removed (Picture in left).
    After removing, new nodes become leaf nodes with value (target = 2) (Picture in center).

    Example 2:

    Input: root = [1,3,3,3,2], target = 3
    Output: [1,3,null,null,2]

    Example 3:

    Input: root = [1,2,null,2,null,2], target = 2
    Output: [1]

    Explanation: Leaf nodes in green with value (target = 2) are removed at each step.

    Constraints:

    • The number of nodes in the tree is in the range [1, 3000].
    • 1 <= Node.val, target <= 1000

    送分题, 就不多做赘述了, 如果左右两个子节点返回的都是 null, 且当前节点的 value == target, 则删除当前节点, 返回 null, 否则的话则将已经完成删除操作的左右子节点重新赋值给当前节点的左右子节点。


    use std::cell::RefCell;
    use std::rc::Rc;
    impl Solution {
        fn dfs(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Option<Rc<RefCell<TreeNode>>> {
            if let Some(node) = root {
                let left = Solution::remove_leaf_nodes(node.borrow_mut().left.take(), target);
                let right = Solution::remove_leaf_nodes(node.borrow_mut().right.take(), target);
                if left.is_none() && right.is_none() && node.borrow().val == target {
                    return None;
                }
                node.borrow_mut().left = left;
                node.borrow_mut().right = right;
                return Some(node);
            }
            None
        }
        pub fn remove_leaf_nodes(
            root: Option<Rc<RefCell<TreeNode>>>,
            target: i32,
        ) -> Option<Rc<RefCell<TreeNode>>> {
            Solution::dfs(root, target)
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • 相关阅读:
    【Java基础】四张图轻松拿捏Java VisualVM添加Visual GC插件实现JVM性能调优
    整理37个超牛逼的Python库,用过一半算你牛
    施工小能手路边石混凝土成型机的工艺原理
    高校教务系统登录页面JS分析——南京邮电大学
    聊聊 HTTP 性能优化
    小程序WebView远程连接报错(1202)网络出错,轻触屏幕重新加载,如何解决这个问题?
    一键自动化博客发布工具,用过的人都说好(infoq篇)
    微信小程序rich-text里面写单行溢出显示省略号在ios中不显示的问题
    2023高教社杯全国大学生数学建模竞赛B题代码解析
    矢量(vector)瓦片与栅格(raster)瓦片对比
  • 原文地址:https://blog.csdn.net/wangjun861205/article/details/126927271
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号