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. C# 显示纯文本对齐封装(控制显示字体长度)

    坑: 用户在写多行的纯文本上来了一个对齐的表格..如下: 原因:不同的字体下,中文,英文大写,英文小写,字符,尤其是空格..字体占用的长度是不一样的,然后显示出来就是乱的.. 然而客户要求在不同的字体 ...

  2. 学习笔记-----《Pattern Recognition and Machine Learning》Christopher M. Bishop

    Preface 模式识别这个词,以前一直不懂是什么意思,直到今年初,才开始打算读这本广为推荐的书,初步了解到,它的大致意思是从数据中发现特征,规律,属于机器学习的一个分支. 在前言中,阐述了什么是模式 ...

  3. [POJ2259]Team Queue (队列,模拟)

    2559是栈,2259是队列,真的是巧啊 题意 模拟队列 思路 水题 代码 因为太水,不想打,发博客只是为了与2559照应,于是附上lyd的std #include <queue> #in ...

  4. [转]Unity-移动设备可用的压缩解压缩源码

    原文:http://www.manew.com/thread-103250-1-1.html 最近在做客户端数据的分离,不希望对项目有什么影响,也不太想用AssetBundle,太麻烦,就在网上找了找 ...

  5. sass快速入门

    sass十分钟入门 变量 sass中可以定义变量,方便统一修改和维护. //sass style //----------------------------------- $fontStack: H ...

  6. JAVA_AesCBC纯净例子

    import java.io.UnsupportedEncodingException; import java.security.InvalidAlgorithmParameterException ...

  7. ssm框架各自的作用

  8. C# 使用WinApi操作剪切板Clipboard

    前言: 最近正好写一个程序,需要操作剪切板 功能很简单,只需要从剪切板内读取字符串,然后清空剪切板,然后再把字符串导入剪切板 我想当然的使用我最拿手的C#来完成这项工作,原因无他,因为.Net框架封装 ...

  9. 适配iOS11

    总结在iOS11系统中出现的适配问题: 启动app发现上下有空隙,不能完全贴合屏幕----- 解决方案:添加一张尺寸为1125x2436的启动图. 隐藏导航栏的界面,会出现无法贴合屏幕顶部(一般来说, ...

  10. 深入解析Java反射基础

    博客原文:http://www.sczyh30.com/posts/Java/java-reflection-1/ - 这老哥写的特别好 一.回顾:什么是反射? 反射(Reflection)是Java ...