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 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;
}
Trim a Binary Search Tree的更多相关文章
- LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)
669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...
- 【Leetcode_easy】669. Trim a Binary Search Tree
problem 669. Trim a Binary Search Tree 参考 1. Leetcode_easy_669. Trim a Binary Search Tree; 完
- Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees
Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees 669.Trim a Binary Search Tr ...
- 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 t ...
- [LeetCode] 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 a ...
- [Swift]LeetCode669. 修剪二叉搜索树 | 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 a ...
- [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 a ...
- 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 t ...
- LeetCode - 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 a ...
- [LeetCode&Python] Problem 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 a ...
随机推荐
- Django 项目重命名
在日常学习工作过程中,我们难免需要复用以前的项目,这里讲下复用 Django 项目并重命名的过程. 1.修改项目名称,使用 pycharm -> refactor 重命名整个项目. 2.修改 m ...
- luoguP3835 [模板]可持久化平衡树
https://www.luogu.org/problemnew/show/P3835 因为博主精力和实力有限,学不懂 fhq treap 了,因此只介绍 leafy tree 解法 leafy tr ...
- SQL Server 数据类型与.Net Framework平台映射
SQL Server 和 .NET Framework 基于不同的类型系统. 例如,.NET Framework Decimal 结构的最大小数位数为 28,而 SQL Server 的 decim ...
- Tarjan 点双+割点+DFS【洛谷P3225】 [HNOI2012]矿场搭建
P3225 [HNOI2012]矿场搭建 题目描述 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤 ...
- js 封装一个均速动画函数
//动画函数---任意一个元素移动到指定的目标位置 //element为元素 target为位置 function carToon(element, target) { //设置一个定时器让他循环去增 ...
- ELK 实用架构
- C++_标准模板库STL概念介绍5-其他库与总结
C++还提供了其他一些类库,这些类库更加专用. 例如,头文件complex为复数提供了类模板complex,包含用于float.long和long double的具体化. 这个类提供了标准的复数运算以 ...
- rest-assured的日志使用介绍
在许多测试用例当中,为了帮助我们创建正确的断言和发送正确的请求,打印出详细的响应和请求数据是非常有用的.为此我们可以使用rest-assured提供的预定义过滤器或者使用其中的一些快捷方法. 一.请求 ...
- npm的介绍
npm使JavaScript开发人员能够轻松地共享和重用代码,并且可以轻松更新你正在共享的代码. 如果你一直在使用JavaScript,你可能已经听说过npm.npm使JavaScript开发人员能够 ...
- C# 关于时区的操作
有关时区自动更新的 在注册表以下路径,start键值3,4表示自动/不自动更新 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\tza ...