[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 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 思路:考的是二叉树节点的删除,只要满足条件的删除就好了,得注意的是一个节点是有两个孩子还是小于两孩子;
/**
* 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;
root.left = trimBST(root.left,L,R); //递归的删除,从底层删起
root.right = trimBST(root.right,L,R);
if (root.val<L||root.val>R){ //这里是如何删除满足条件的节点的代码
if (root.left!=null&&root.right!=null){
root.val = findMax(root.left).val; //这是有两个孩子的情况
trimBST(root.left,root.val-1,root.val+1);
}
else { //只有一个孩子或无孩子
if (root.left==null)
root = root.right;
else if (root.right==null)
root = root.left;
}
}
return root;
}
public TreeNode findMax(TreeNode root){
if (root==null)
return null;
if (root.right==null)
return root;
else
return findMax(root.right);
}
}
[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 t ...
- LeetCode: 669 Trim a Binary Search Tree(easy)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
- LeetCode 669. Trim a Binary Search Tree修剪二叉搜索树 (C++)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
- [leetcode]669. Trim a Binary Search Tree寻找范围内的二叉搜索树
根据BST的特点,如果小于L就判断右子树,如果大于R就判断左子树 递归地建立树 public TreeNode trimBST(TreeNode root, int L, int R) { if (r ...
- 【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 ...
- 【LeetCode】669. Trim a Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- [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 ...
- 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 ...
随机推荐
- 创建线程的一般方式和匿名内部类方式对比——继承thread类,重写run()方法
第一种:继承thread类,重写run()方法 一般方式:Demo01.java /** * 创建线程的第一种方式:继承thread类,重写run()方法 * * @author :liuqi * @ ...
- HeadDoc自动注释语法
记录于2013/4/23: 关于HeaderDoc注释和标签的简要介绍 每个HeaderDoc注释可分为顶级标签和第二级标签: (1)顶级标签:宣布API的声明类型 (function, struct ...
- 勾勾街:用最小的成本封装一个苹果IOS APP! 封装技术再度升级~
勾勾街自上线以来,“遭到”大量群众的喜爱... 只能用遭到这个词儿,因为大家好像都被憋了很久了,哈哈哈! 我们的技术是先进的,也是首创的,但最近发现了另一个网站,把我们的技术抄走了.... 本来这个事 ...
- 利用Github免费搭建个人主页(转)
搭建过程涉及: Github注册 Github搭建博客 域名选购 绑定域名 更多 一. Github注册 在地址栏输入地址:http://github.com/join填写相关信息, 按步骤完成即可 ...
- restful levels
1. 什么是RESTful REST这个词,是Roy Thomas Fielding在他2000年的博士论文中提出的.翻译过来就是"表现层状态转化.” REST是一种软件架构风格.设计风格, ...
- List使用linq的OrderBy方法排序,并按照两个字段排序的写法
SfaMember.GetList(searchInfo, 0, 1000, out Allcount).Where(item => item.bOpen == true).OrderBy(it ...
- Linux用命令启动程序(eclipse、IDEA等)
打开根目录用管理员权限打开HOME 找到下图截图中的框选出的文件 用文本编辑器打开后 在文件末尾添加所需要打开的应用文件所在的目录 这里以本人的IDEA和eclipse为例:
- React的类型检测PropTypes
React.propTypes:React.PropTypes 提供很多验证器来验证传入数据的有效性,当向props传入无效数据时,JavaScript 控制台会抛出警告. ; class MyTit ...
- 样式布局与 BFC
一.几类视图 内联视图:inline 单行 块级视图:block 换行,有高度 行内块级视图:inline-block 单行,有高度 二.几类布局 块级布局 换行,通过设置 margin 水平居中 & ...
- JavaWeb学习路线
一.三大组件介绍 javaweb在开发中有三大组件分别提供不同的功能,这三大组件为servlet,filter,listener 1.servlet 简单来说就是客户端请求服务器和接受服务器的响应,狭 ...