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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. [leetcode]669. Trim a Binary Search Tree寻找范围内的二叉搜索树

    根据BST的特点,如果小于L就判断右子树,如果大于R就判断左子树 递归地建立树 public TreeNode trimBST(TreeNode root, int L, int R) { if (r ...

  5. 【Leetcode_easy】669. Trim a Binary Search Tree

    problem 669. Trim a Binary Search Tree 参考 1. Leetcode_easy_669. Trim a Binary Search Tree; 完

  6. 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 ...

  7. 【LeetCode】669. Trim a Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. (转)Jquery获取上级、下级或者同级的元素

    下面介绍JQUERY的父,子,兄弟节点查找方法 jQuery.parent(expr) 找父亲节点,可以传入expr进行过滤,比如$("span").parent()或者$(&qu ...

  2. dotnet core 3.0 linux 部署小贴士

    dotnet core 3.0 目前还是测试版,在linux下安装 sdk 需要有一些注意事项 1.下载url https://dotnet.microsoft.com/download/thank- ...

  3. DW1000 用户手册中文版 附录3:双向测距(Two-Way Ranging)

    由于已经在wode中排版无法直接复制到博客中,故本节博客发布使用了图片. 论坛可下载PDF  http://bphero.com.cn/forum.php?mod=viewthread&tid ...

  4. 蛤?你要用html做游戏?(笔记版)

    标签(空格分隔):canvas html game 本书是看<html5 Canvas游戏开发实战>(2013)笔记 博主小白,啥也不懂类型,这只是一个笔记,需要的话可以看原书. 书张这样 ...

  5. HTML入门10

    目前,掌握了图像,视频和音频的嵌入,下面来谈iframe和embed.object嵌入网页, 嵌入简史,刚开始流行用嵌入框架然后不同部分显示i不同内容,可以解决下载速度慢时的问题: 慢慢的插件技术流行 ...

  6. linux android开发环境搭建

    android开发环境搭建的一些有用链接:1.sdk manager的国内服务器http://www.cnblogs.com/huangjacky/p/4077982.html2.常见问题的解决htt ...

  7. django+javascrpt+python实现私有云盘代码

    丁丁:由于篇幅有限,这里暂时只展示python后端代码,前端js代码后面上传,有需要的也可以留言私信我. 1.view.py 使用用户.部门.公司等相关账号的创建,已经个人,部门账号的冻结,删除,相关 ...

  8. Pyinstaller (python打包为exe文件)

    需求分析: python脚本如果在没有安装python的机器上不能运行,所以将脚本打包成exe文件,降低脚本对环境的依赖性,同时运行更加迅速. 当然打包的脚本似乎不是在所有的win平台下都能使用,wi ...

  9. Python-数据类型1

    在Python中常见的数据类型有:整数(int).字符串(str).小数/浮点数(float).列表.元组.字典和布尔类型等,下面会进行一一介绍. 整数和小数,不用多介绍相信大家都有所了解,字符串是用 ...

  10. LAMP构架搭建论坛

    安装MYSQL数据库服务程序       LAMP动态网站部署架构是一套由Linux+Nginx+MYSQL+PHP组成的动态网站系统解决方案,具有免费.高效.扩展性强且资源消耗低等优良特性.使用手工 ...