给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。

错误的答案:

class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(root == NULL)
return NULL;
if(root ->val < L)
{
root = root ->right;
trimBST(root, L, R);
}
else if(root ->val > R)
{
root = root ->left;
trimBST(root, L, R);
}
else
{
root ->left = trimBST(root ->left, L, R);
root ->right = trimBST(root ->right, L, R);
}
return root;
}
};

正确的答案:

class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(root == NULL)
return NULL;
if(root ->val < L)
{
root = trimBST(root ->right, L, R);
}
else if(root ->val > R)
{
root = trimBST(root ->left, L, R);
}
else
{
root ->left = trimBST(root ->left, L, R);
root ->right = trimBST(root ->right, L, R);
}
return root;
}
};

Leetcode669.Trim a Binary Search Tree修建二叉树的更多相关文章

  1. LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)

    669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...

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

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

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

  4. [Swift]LeetCode669. 修剪二叉搜索树 | 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 ...

  5. [LeetCode] 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 ...

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

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

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

  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. Error-IDEA:“Import from external model” 与 “Create from existing source”的区别

    ylbtech-Error-IDEA:“Import from external model” 与 “Create from existing source”的区别 1.返回顶部 1. “Import ...

  2. 使用python和tableau对数据进行抓取及可视化

    使用python和tableau对数据进行抓取及可视化 本篇文章介绍使用python抓取贷款及理财平台的数据,并将数据拼接和汇总.最终通过tableau进行可视化.与之前的python爬虫文章 不同之 ...

  3. SPSS超详细操作:分层回归(hierarchical multiple regression)

    SPSS超详细操作:分层回归(hierarchical multiple regression) 1.问题与数据 最大携氧能力(maximal aerobic capacity, VO2max)是评价 ...

  4. varStatus实现<c:forEach获取元素的下标 集合长度

    有时候需要获取元素的下标,来实现一个编号的效果 那么使用 varStatus可以实现 代码 效果 如果需要把序号倒着数 那就用集合的长度减去当前下标 首先引入 <%@ taglib prefix ...

  5. idea debug技巧

    1 给对象设置值

  6. [转]WPF--模板选择

    典型的,把模板关联到一块特定的数据上,不过通常希望动态的确定使用哪个模板---既可以基于一个属性值,也可以是一个全局状态.当真正需要大规模替换模板时,也可以使用DataTemplateSelector ...

  7. pg_hba.conf配置文件

    实例级别的权限由pg_hba.conf来控制,例如 : # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix doma ...

  8. (二)通过JAVA调用SAP接口 (增加一二级参数)

    (二)通过JAVA调用SAP接口 (增加一二级参数) 一.建立sap连接 请参考我的上一篇博客 JAVA连接SAP 二.测试项目环境准备 在上一篇操作下已经建好的环境后,在上面的基础上新增类即可 三. ...

  9. 装配SpringBean(二)--XML方式介绍

    装配SpringBean,我理解的意思就在容器中定义一个bean,然后让容器通过某种方式找到它.因为在Spring中一切皆资源,资源就是所谓的bean,我们怎么才能从容器中获取这些资源呢?那就是控制反 ...

  10. Chapter 1 线性表

    Preface: 这部分主要是我考研之前的总结,留作以后用到时再翻出来看看.都是基础的知识点,望各(Da)位(Lao)不喜勿喷,欢迎指正. 适用人群:考研复习过一遍数据结构的,可以看看,查漏补缺(顺便 ...