[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 ...
随机推荐
- NOIP-Vigenère密码
题目描述 16 世纪法国外交家 Blaise de Vigenère 设计了一种多表密码加密算法―― Vigenère 密码. Vigenère 密码的加密解密算法简单易用,且破译难度比较高,曾在美国 ...
- tomcat安装启动startup.bat文件命令行界面出现乱码的问题解决
进入tomcat安装界面,进入conf文件夹,找打logging.properties,打开进行编辑,在最后添加一句 java.util.logging.ConsoleHandler.encoding ...
- Java for Android 第二周课上实验一
(一)命令行下程序开发 (二)IDEA下程序开发调试 Mac OS系统下使用的IDEA为 Netbeans (三)测试题我的学号后两位为10 使用简单的PHP小程序得我的题目为2:实现简单四则运算(能 ...
- Spring源码学习-容器BeanFactory(五) Bean的创建-探寻Bean的新生之路
写在前面 上面四篇文章讲了Spring是如何将配置文件一步一步转化为BeanDefinition的整个流程,下面就到了正式创建Bean对象实例的环节了,我们一起继续学习吧. 2.初始化Bean对象实例 ...
- 搭建 RTMP 服务器
主要步骤 具体步骤 FAQ docker 搭建版 参考 主要步骤 下载 nginx 的 rtmp 模块 编译nginx,带 hls,rtmp 配置 nginx.conf,设置 rtmp 的推流文件路径 ...
- 微信小程序ios点击状态栏返回顶部不好使
最近做了一款微信小程序,各方面感觉都很完美(萝卜一直这么自信),今天设计总监告诉我你的小程序怎么返回顶部不好使呀,吓得我赶紧拿手机试试,没毛病啊,我手机(苦逼的安卓机)上点两下就回去了呀,遂去找他理论 ...
- 第二次Srum冲刺
一.项目简介 1.项目名称:云医院智能管理系统 2.项目介绍:该项目涵盖了目前医院里的一些基本需求,由于时间和技术有限,先暂时列出如下图所示的一些要实现的功能,关于实现的过程,还需在后面的学习当中不断 ...
- Win10 安装 VMWare中 MAC OS X的安装,VMWare tools的配置与iOS的Helloworld
iOS的开发必须在MAC OS X系统下进行,这很蛋疼,现在MACBOOK动不动就上千上万大洋,这足够买台配置怪兽了,好吗?然而,我们是可以通过在VMWare中安装MAC OS X进行iOS开发的.对 ...
- 【RL-TCPnet网络教程】第37章 RL-TCPnet之FTP客户端
第37章 RL-TCPnet之FTP客户端 本章节为大家讲解RL-TCPnet的FTP客户端应用,学习本章节前,务必要优先学习第35章的FTP基础知识.有了这些基础知识之后,再搞本章节会有事 ...
- Go语言数组和切片的原理
目录 数组 创建 访问和赋值 切片 结构 初始化 访问 追加 拷贝 总结 数组和切片是 Go 语言中常见的数据结构,很多刚刚使用 Go 的开发者往往会混淆这两个概念,数组作为最常见的集合在编程语言中是 ...