根据BST的特点,如果小于L就判断右子树,如果大于R就判断左子树

递归地建立树

public TreeNode trimBST(TreeNode root, int L, int R) {
if (root==null) return null;
if (root.val<L) return trimBST(root.right,L,R);
if (root.val>R) return trimBST(root.left,L,R);
TreeNode res = new TreeNode(root.val);
res.left = trimBST(root.left,L,R);
res.right = trimBST(root.right,L,R);
return res;
}

[leetcode]669. Trim a Binary Search Tree寻找范围内的二叉搜索树的更多相关文章

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

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

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

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

  5. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  6. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  7. [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  8. PAT A1099 Build A Binary Search Tree (30 分)——二叉搜索树,中序遍历,层序遍历

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  9. Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

随机推荐

  1. react高阶组件的一些运用

    今天学习了react高阶组件,刚接触react学习起来还是比较困难,和大家分享一下今天学习的知识吧,另外缺少的地方欢迎补充哈哈 高阶组件(Higher Order Components,简称:HOC) ...

  2. Spring Boot 项目瘦身指南,瘦到不可思议!129M->1.3M

    之前在 从使用传统Web框架到切换到Spring Boot后的总结 中提到关于 Spring Boot 编译打包,Spring Boot 应用程序不用额外部署到外部容器中,可以直接通过 Maven 命 ...

  3. IdentityServer4系列 | 授权码模式

    一.前言 在上一篇关于简化模式中,通过客户端以浏览器的形式请求IdentityServer服务获取访问令牌,从而请求获取受保护的资源,但由于token携带在url中,安全性方面不能保证.因此,我们可以 ...

  4. pandas 移动列的方法

    import pandas as pd df = pd.DataFrame(np.random.randn(3,4),columns=['a','b','c','d']) k = df.pop(&qu ...

  5. java8+ Lambda表达式基本用法

    LIST public class LambdaTest { @Getter @Setter @AllArgsConstructor static class Student{ private Lon ...

  6. AtCoder Regular Contest 108

    Contest Link Official Editorial A - Sum and Product Given are integers \(S\) and \(P\) . Is there a ...

  7. OpenCV Error: Assertion failed (src.size == dst.size && src.channels() == dst.channels()) in cvConvertScale

    发现问题:在做kinect采集的深度图去噪的时候遇到了cvConvertScale格式转换的问题. OpenCV Error: Assertion failed (src.size == dst.si ...

  8. js中数组、字符串、日期、数学API方法一览

    以下内容摘选自 http://www.w3school.com.cn/jsref/jsref_obj_array.asp 点击方法新窗口打开详解 数组: 方法 描述 concat() 连接两个或更多的 ...

  9. mysql主从同步错误

    一.主从同步报错 mysql> show slave status\G; *************************** 1. row ************************* ...

  10. svn提交时提示 Aborting commit: remains in conflict 解决办法,更改svn服务地址

    TortoiseSVN客户端如何更改新的URL 问题: 我们的服务器换了新的URL地址,这时候我们本地的SVN访问帐号和地址就要重新定义了. 解决步骤: 1:重新定义SVN的URL,右键(Tortoi ...