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;
}

最新文章

  1. bzoj1441 MIN
  2. jquery的ajax同步和异步的理解及示例
  3. 禁用backspace键的后退功能
  4. 移动端rem布局
  5. 测试WWW方案(反向代理,负载均衡,HTTP加速缓存)
  6. solr和mongodb比较
  7. 基于visual Studio2013解决C语言竞赛题之1048打印矩阵
  8. 【API调用】腾讯云短信
  9. GHSpro多数据库连接
  10. xampp集成环境下重置mysql的密码
  11. 《DSP using MATLAB》Problem 7.26
  12. 巴黎游戏周: PS4独占游戏《重力少女2》
  13. JavaScript简单简介
  14. thinkphp 攻略
  15. Linux文件检索
  16. PyQT的安装和配置
  17. Go实战--也许最快的Go语言Web框架kataras/iris初识(basic认证、Markdown、YAML、Json)
  18. 42. oracle通过两张表的一个字段对应,update其中一张表的某个字段
  19. jrebel
  20. ODS设计

热门文章

  1. C# 在Winform设计一个耗时较久的任务在后台执行时的状态提示窗口
  2. spring 学习(一):使用 intellijIDEA 创建 maven 工程进行 Spring ioc 测试
  3. 【noip2017】【Luogu3960】列队 线段树
  4. Udp 网络字节序
  5. 【问题记录】Python运行报错:can only concatenate str (not &quot;int&quot;) to str
  6. JAVA Web从前端到后台常用框架介绍
  7. linux的目录和基本的操作命令
  8. Angular 组件 mat-paginator 自定义详细用法
  9. Kibana6.x.x源码结构分析笔记
  10. JS中||的某些用法