Trim a Binary Search Tree
2024-10-15 23:11:30
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.
Example 1:
Input:
1
/ \
0 2 L = 1
R = 2 Output:
1
\
2
Example 2:
Input:
3
/ \
0 4
\
2
/
1 L = 1
R = 3 Output:
3
/
2
/
1
public TreeNode trimBST(TreeNode root, int L, int R) {
if(root == null){
return null;
}else if(root.val >= L && root.val <= R){
root.left = trimBST(root.left, L, R);
root.right = trimBST(root.right, L, R);
return root;
}else if(root.val < L){
return trimBST(root.right, L, R);
}else if(root.val > R){
return trimBST(root.left, L, R);
}
return null;
}
最新文章
- bzoj1441 MIN
- jquery的ajax同步和异步的理解及示例
- 禁用backspace键的后退功能
- 移动端rem布局
- 测试WWW方案(反向代理,负载均衡,HTTP加速缓存)
- solr和mongodb比较
- 基于visual Studio2013解决C语言竞赛题之1048打印矩阵
- 【API调用】腾讯云短信
- GHSpro多数据库连接
- xampp集成环境下重置mysql的密码
- 《DSP using MATLAB》Problem 7.26
- 巴黎游戏周: PS4独占游戏《重力少女2》
- JavaScript简单简介
- thinkphp 攻略
- Linux文件检索
- PyQT的安装和配置
- Go实战--也许最快的Go语言Web框架kataras/iris初识(basic认证、Markdown、YAML、Json)
- 42. oracle通过两张表的一个字段对应,update其中一张表的某个字段
- jrebel
- ODS设计
热门文章
- C# 在Winform设计一个耗时较久的任务在后台执行时的状态提示窗口
- spring 学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试
- 【noip2017】【Luogu3960】列队 线段树
- Udp 网络字节序
- 【问题记录】Python运行报错:can only concatenate str (not ";int";) to str
- JAVA Web从前端到后台常用框架介绍
- linux的目录和基本的操作命令
- Angular 组件 mat-paginator 自定义详细用法
- Kibana6.x.x源码结构分析笔记
- JS中||的某些用法