给定一个二叉搜索树,同时给定最小边界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. 主机入侵防御系统(HIPS)分析

    主机入侵防御系统(Host Intrusion Prevent System,HIPS)是近几年出现并迅速发展的新兴产物,与传统意义的防火墙和杀毒软件不同,它并不具备特征码扫描和主动杀毒等功能,所以想 ...

  2. Python学习day38-并发编程(线程)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  3. Activiti历史查看

    流程执行完毕后,究竟去了哪里有些疑问. 虽然已完成的任务在act_ru_task和act_ru_execution表中都已被删除,但是这些数据还存在activiti的数据库中,作为历史改由Histor ...

  4. springboot-actuator监控的401无权限访问

    在pom.xml里边添加 <dependency> <groupId>org.springframework.boot</groupId> <artifact ...

  5. Linux常用知识

    1.Redhat 系统按如下系统启动:加载内核执行init程序/etc/rc.d/rc.sysinit #由init执行的第一个脚本/etc/rc.d/rc${RUNLEVEL}d/* #$RUNLE ...

  6. vue.js组件的个人总结

    vue.js的组件使用过程分为三个步骤:1.创建组件构造器: 2.注册组件: 3.使用组件 组件同时也分为全局组件与局部组件 1.全局组件 2.局部组件 注意:由于 HTML 标签不区分大小写,所以在 ...

  7. node概览和安装

    一.node是一个平台环境,可以运行js代码的服务器端平台. 设计最初node是用来解决并发问题的,现在可以用来放在服务端使用. node平台的有优点:运行速度快,支持高并发,轻便.小巧 但是与jav ...

  8. new运算符与malloc函数(还需要修改)

    细说new与malloc的10点区别 C++ 自由存储区是否等价于堆? 浅谈new/delete和malloc/free的用法与区别 new和malloc都是在对上开辟内存,但尽量使用new. 使用m ...

  9. 错误 2 error C2059: 语法错误:“::”

    设置项目属性,在预定义处理器中添加定义NOMINMAX来禁止使用Vsual C++的min/max宏定义. 项目属性   ——> C/C++ ——> 预处理器 ——> 预处理器定义 ...

  10. 你真的了解ES6的promise吗?

    promise是一个构造函数,是用来解决ajax回调地狱的问题.axios就是用promise封装的.用于解决ajax请求时出现的回调地狱的问题.异步伴随回调. const p1 = new Prom ...