[LeetCode] 669. Trim a Binary Search Tree

LeetCode --- 669. Trim a Binary Search Tree 解题报告 Given a binary search tree and the lowest and highest boundaries asLandR, trim the tree so that all its elements lies in[L, R](R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary se... 阅读详情

原题链接: https://leetcode.com/problems/trim-a-binary-search-tree/

1. 题目介绍

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.
给出一个二叉树,以及一个范围 [L , R]。要求所有的二叉树节点都在 [L, R] 的范围里面。如果发现不在该范围内的节点,就要把它修剪掉。这种修剪可能会改变二叉树的根节点,如果真的把根节点给剪掉了,返回的结果需要是新的根节点。
在这里插入图片描述
在这里插入图片描述

2. 解题思路

这道题本质上仍然是一道深度优先搜索的题目。同样可以使用DFS+递归的方法做。
首先需要知道二叉树的一个性质:左子树的所有节点都小于根节点,右子树的所有节点都大于根节点。因此本题所谓的修剪过程,就是不断把大于R的节点换成它的左子节点,把小于L的节点换成它的右子节点的过程。
可以把函数 trimBST(TreeNode root, int L, int R) 的作用理解为帮助树root 寻找在 [L,R] 之间的根节点。
如果root.val > R,那么就在它的左子树中寻找符合要求的根节点;
如果root.val < L,那么就在它的右子树中寻找符合要求的根节点。
如果root.val 本身就在[L,R]之内,说明它已经符合要求了,那么就进一步帮它在左子树和右子树中寻找满足要求的根节点。

实现代码

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode trimBST(TreeNode root, int L, int R) {
        if(root == null){
            return null;
        }
        if(root.val < L){
            return trimBST(root.right, L, R);
        }
        if(root.val > R){
            return trimBST(root.left , L, R);
        }
        root.left  = trimBST(root.left ,L,R);
        root.right = trimBST(root.right,L,R);
        return root;
    }
}
无人机蜂群联合指挥作战技术详解 随着人工智能、通信技术、新材料等领域的不断进步,无人机蜂群联合指挥作战技术将迎来更加广阔的发展前景。未来,无人机蜂群将更加智能化、自主化,能够执行更加复杂、精细的作战任务。同时,加强国际合作,共同制定国际规则和标准,将是推动无人机蜂群技术健康发展的重要方向。无人机蜂群技术,是指通过先进的通信技术、自主导航与决策算法,将大量小型、低成本、功能各异的无人机组织成一个高度协同的群体,执行复杂多样的军事任务。2. 通信与数据链:采用高速、低延迟、抗干扰的通信技术,保障群体内无人机间的信息交换。 阅读详情

相关推荐

MATLAB 论文复现——基于虚拟储能的电动汽车充放电调度策略及激励评价

文章基于虚拟储能聚合体仿真模型优化电动汽车充放电调度,并采用复合权重评价电动汽车的充电激励。根据电动汽车灵活性和系统稳定性衡量其贡献大小,并通过模糊推理系统模拟用户的充电意愿,实现对电动汽车充电的引导和优化。[1]陈朔,胡昊,方慧敏,等.基于虚拟储能的电动汽车充放电调度策略及激励评价[J/OL].系统仿真学报,1-12[2024-07-24].https://doi.org/10.16182/j.issn1004731x.joss.23-1316.基于虚拟储能的电动汽车充放电调度策略及激励评价。

sjr1478的博客 367

leetcode 669. Trim a Binary Search Tree

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the r

淡然坊 2316

逆向实战:Soul聊天协议加密与Frida绕过检测

本文详细解析了Soul聊天协议逆向分析的全过程,重点介绍了如何利用Frida绕过应用的反调试检测机制。通过静态分析定位关键SO库,并编写Frida脚本动态Hook线程创建函数,成功实现对抗。文章还进一步追踪了基于DES算法的加密流程,并探讨了协议数据结构的解析方法,为移动应用安全研究提供了实战参考。

2w3e4r5t6y的博客 1141

LeetCode //C - 669. Trim a Binary Search Tree

【代码】LeetCode //C - 669. Trim a Binary Search Tree。

Made in Code 1216

Moltemplate建模----水盒子data文件

1功能介绍 Moltemplate是LAMMPS官方支持的建模工具之一,适用于LAMMPS建模。该程序创建了一种简单的文件格式来存储分子定义和力场,即模板LT。LT文件包含与特定分子有关的所有文本(包括坐标,键拓扑,角度,力场参数,约束,组和修复)。 Moltemplate可以复制分子,对其进行自定义,然后用它作为构建更大、更复杂分子的基础。构建后,可以自定义...

元素魔方科研服务 2603

从理论到代码:手把手教你用Matlab复现MMC的HSS阻抗模型(附避坑指南)

本文详细介绍了如何使用Matlab复现MMC的HSS阻抗模型,涵盖谐波状态空间(HSS)理论、代码实现步骤及常见问题解决方案。通过谐波线性化和序阻抗建模技术,帮助工程师准确分析MMC的阻抗特性,并提供避坑指南以解决矩阵维度错误、收敛性问题和频域转换陷阱等难点。

weixin_42561040的博客 365

【Leetcode】669. Trim a Binary Search Tree

题目地址: https://leetcode.com/problems/trim-a-binary-search-tree/ 给定一棵二叉搜索树,再给定一个范围[L,R][L,R][L,R],要求将这棵树所有在这个范围之外的节点都删掉,返回新树树根。 思路是用分治法。如果是空树则直接返回,否则看树根的范围,如果树根处于(−∞,L)(-\infty, L)(−∞,L),那说明树根以及整个左子树都要被...

数学、算法爱好者的博客 205

1020空心杯电机驱动设计与ESP32实战

空心杯电机是一种高动态响应的直流微特电机,其驱动本质是解决电感负载下的大di/dt启动、反电动势耦合与电流纹波抑制问题。核心原理在于建立包含内阻、电感和反电动势的等效电路模型,并据此匹配开关器件、PWM参数与采样闭环。技术价值体现在小体积下实现毫秒级响应、低噪声(>30kHz)、高精度电流保护(±50mA)及热鲁棒性。典型应用于微型航模、手持云台与精密执行器等对功率密度和动态性能敏感的嵌入式系统。本文以1020规格空心杯电机为对象,基于ESP32平台完成从MOSFET选型、32kHz硬件PWM生成到INA2

weixin_42298164的博客 598

[leetcode]669. Trim a Binary Search Tree(Python)

[leetcode]669. Trim a Binary Search Tree题目描述题目理解解题思路递归Time 题目描述 Category:Easy Tree Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements ...

La_Ragazza的博客 442

ubuntu 24.04安装ros1 noetic

上面是参考链接给的修改内容。除了上面要修改的地方,我在编译的生活还有一个地方报错,修改成如下形式。因为我已经对文件rosconsole_log4cxx.cpp作了上面的修改,所有是不是188行不太确认了,如果报“log4cxx::spi::LocationInfo”问题,按照下面修改就可以了。我在编译时还报了一个问题,就是我之前安装的是jazzy。所以在编译时会报ros变量名不对,这时对变量名做修改就可以了。具体怎么修改,百度。a、将源文件中所有CMakelist中编译标准改为c++ 17.

yangpan011的博客 3215

字符编码——简体中文编码中区位码、国标码、内码、外码、字形码的区别及关系

目录区位码国标码(交换码)内码(机内码)区位码、国际码、内码的转换为什么要加上20H为什么加上80H 本文以GB2312为例,讲述区位码、国标码、内码、外码、字形码 区位码 整个GB2312字符集分成94个区,每区有94个位,每个区位上只有一个字符,即每区含有94个汉字或符号,用所在的区和位来对字符进行编码(实际上就是字符编号、码点编号),因此称为区位码。 换言之,GB2312将包括汉字在内的所有字符编入一个94 * 94的二维表,行就是“区”、列就是“位”,每个字符由区、位唯一定位,其对应的区、位编号合并

路人coder 5382

[leetcode] 669. Trim a Binary Search Tree

Description Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tre...

Learning from the mistakes 182

42、多媒体与虚拟现实在艺术和影视领域的创新应用

本文探讨了多媒体与虚拟现实在艺术和影视领域的创新应用,涵盖三个典型案例:影视与人类学融合项目通过协作模式模糊作者与观众界限,推动文化文献的发展;交互式数字短片《分手》利用基于对象的广播技术探索个性化影视体验;威尼斯双年展瑞士馆4D项目则通过虚拟现实技术实现文化遗产的数字化展示。这些案例展示了技术在叙事、用户体验及文化保护中的深度应用,并展望了未来技术融合、个性化深化与文化遗产传承的发展趋势。

kappa的博客 117

[LeetCode] 669.Trim a Binary Search Tree

[LeetCode] 669.Trim a Binary Search Tree 题目描述 解题思路 实验代码 题目描述Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L).

user_name_is_so_long的博客 291

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

[Leetcode]669 Trim a Binary Search Tree

Given a binary search tree and the lowest and highest boundaries asLandR, trim the tree so that all its elements lies in[L, R](R >= L). You might need to change the root of the tree, so t...

Nicole852217677的专栏 107

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【LeetCode】669.Trim a Binary Search Tree解题报告

【LeetCode】669.Trim a Binary Search Tree解题报告tags: Tree题目地址:https://leetcode.com/problems/trim-a-binary-search-tree/description/ 题目描述:   Given a binary search tree and the lowest and highest boundarie

郝春雨的博客 251

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

Leetcode 669. Trim a Binary Search Tree (python+cpp)

Leetcode 669. Trim a Binary Search Tree题目解析: 题目 解析: 如果当前节点在范围内,此节点不动,递归到下一层的左右子树 否则,根据当前节点的范围,直接返回左子树或者右子树 python代码如下: class Solution: def trimBST(self, root: TreeNode, L: int, R: int) -> TreeNode: def trim(node): if not node:

qq_37821701的博客 224

LeetCode-669. Trim a Binary Search Tree [C++][Java]

Given therootof a binary search tree and the lowest and highest boundaries aslowandhigh, trim the tree so that all its elements lies in[low, high]. Trimming the tree shouldnotchange the relative structure of the elements

贫道绝缘子的博客 151

LeetCode669. Trim a Binary Search Tree

【代码】LeetCode669. Trim a Binary Search Tree。

叶卡捷琳堡的博客 361

[LeetCode]669. Trim a Binary Search Tree

[LeetCode]669. Trim a Binary Search Tree 题目描述 思路 递归 当前节点值未在规定范围内时,递归更新当前节点值到范围后,之后递归更新当前节点的左右子树 代码 #include using namespace std; struct TreeNode { int val; TreeNode* left; Tr

Lcharon的博客 475

leetcode 669 Trim a Binary Search Tree

Java class Solution { public TreeNode trimBST(TreeNode root, int L, int R) { if(root==null) return null; if(root.val<L) return trimBST(root.right,L,R); if(root.va

MRxjh的博客 234

LeetCode 669. Trim a Binary Search Tree

669. Trim a Binary Search Tree class Solution { public: TreeNode* trimBST(TreeNode* root, int L, int R) { if(root == NULL) return NULL; if(root-&gt;val &gt;R) return t...

peanWang的博客 143
上一篇: [LeetCode] 965. Univalued Binary Tree
下一篇: [LeetCode] 700. Search in a Binary Search Tree
Ber03
博客等级 码龄9年 7粉丝 169原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值