给定一个二叉搜索树,同时给定最小边界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. fiddler报错:creation of the root certificate was not successful 证书安装不成功

    fiddler提示:creation of the root certificate was not successful 证书安装不成功 首先 找到Tools——>Options 在弹出的菜单 ...

  2. Cesium官方教程12--材质(Fabric)

    原文地址:https://github.com/AnalyticalGraphicsInc/cesium/wiki/Fabric 介绍 Fabric 是Cesium中基于JSON格式来描述materi ...

  3. 机器学习中常用的距离及其python实现

    1 概述 两个向量之间的距离(此时向量作为n维坐标系中的点)计算,在数学上称为向量的距离(Distance),也称为样本之间的相似性度量(Similarity Measurement).它反映为某类事 ...

  4. Codeforces-348E Pilgrims

    #4342. CF348 Pilgrims 此题同UOJ#11 ydc的大树 Online Judge:Bzoj-4342,Codeforces-348E,Luogu-CF348E,Uoj-#11 L ...

  5. [转]【全面解禁!真正的Expression Blend实战开发技巧】第八章 FluidMoveBehavior完全解析之一漂浮移动

    好久没更新博客了,今天如果没急事,准备连发三篇,完全讲解Blend最牛的元素-“FluidMoveBehavior”.我向大家保证这三章一定非常精彩,不看你肯定后悔.我相信这三篇文章发表后,国内很多s ...

  6. 廖雪峰Java9正则表达式-2正则表达式进阶-5非贪婪匹配

    1.贪婪匹配 问题:给定一个字符串表示的数字,判断该数字末尾0的个数? "123000": 3个0 "10100": 2个0 "1001": ...

  7. 我的js运动库新

    1.一些样式的获取和设置 //通过id获取当前元素 //params:id function $id(id) { return document.getElementById(id); } //向cs ...

  8. 省际联动distpicker插件的使用讲解

    1.在使用input页面加载script的引用 <script src="js/distpicker/distpicker.data.js"></script&g ...

  9. (转载)关于My97 datepicker与Angular ng-model绑定问题解决。

    转载自 http://zerosoft.blog.51cto.com/679447/1611403 <input type="text" ng-model="d&q ...

  10. ssh小知识

    1.查看系统在线用户 [root@testdb ~]# w 14:30:26 up 38 days, 21:22, 3 users, load average: 0.00, 0.01, 0.05USE ...