给定一个二叉搜索树,同时给定最小边界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. opencv-阈值分割

    关于自适应阈值,可参考:Wellner 自适应阈值二值化算法 一.大津法OTSU(最大类间方差法) 参考:非黑即白——图像分割入门篇之Otsu阈值 自适应阈值分割—大津法(OTSU算法)C++实现 灰 ...

  2. kafka分析

    目录 1,kafka简介 2, Kafka Server 2.1,kafka中zookeeper的作用 2.2, Broker 2.2.1,Broker高性能设计 2.2.2,Broker选举机制 2 ...

  3. 第一周课堂笔记3th

    1.bool值    True正确                False错误   数字为0时的显示  为空值时“” 2. str   int   bool布尔值  之间的转化 str   int  ...

  4. Apache Pig学习笔记(二)

     主要整理了一下,pig里面的一些关键词的含义和用法,pig虽然是一种以数据流处理为核心的框架,但数据库的大部分关键词和操作,在pig里面基本上都能找到对应的函数,非常灵活与简洁,春节前的最后一篇文章 ...

  5. Redis List类型学习

  6. Numpy数据的操作 * dot() multiply() 的区别

    使用numpy时,跟matlab不同: 1.* dot() multiply() 对于array来说,* 和 dot()运算不同 *是每个元素对应相乘 dot()是矩阵乘法 对于matrix来说,*  ...

  7. Range的范围

    range(N,M)的范围应为: [N,M) 例如:

  8. Bash新技能

    1. 输出数组全部元素 echo ${array_name[@]} 2. 输出数组长度 echo ${#array_name[@]} #获得数组长度 echo ${#string_name} #获得字 ...

  9. 转:Linux 2.4.x内核软中断机制

    源地址:http://www.ibm.com/developerworks/cn/linux/kernel/interrupt/ Linux 2.4.x内核软中断机制 杨沙洲 (pubb@163.ne ...

  10. 装配SpringBean(一)--依赖注入

    所谓依赖注入,我觉得说白了其实就是给成员变量赋值,不管这个成员变量是基本类型还是引用类型,Spring中常用的依赖注入方式有两种: 1.构造器注入 2.setter注入 下面通过代码实例说明这两种注入 ...