题目:

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.

Example 1:

Input:
1
/ \
0 2 L = 1
R = 2 Output:
1
\
2

Example 2:

Input:
3
/ \
0 4
\
2
/
1 L = 1
R = 3 Output:
3
/
2
/
1

分析:

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

二叉搜索树树的性质是,左子树上所有结点的值均小于它的根结点的值,右子树上所有结点的值均大于它的根结点的值,它的左、右子树也分别为二叉搜索树。

所以如果当前的节点的值小于L的话,我们就要递归执行当前节点的右子树,因为左子树上所有节点的值也均小于当前节点的值,自然也小于L。同理如果当前的节点的值大于R的话,就要递归执行当前节点的左子树,因为左子树上节点的值才可能在范围内。这两种情况当前节点都是需要改变的。之后递归执行左右子树即可。

程序:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(root == nullptr) return root;
if(root->val < L) return trimBST(root->right, L, R);
if(root->val > R) return trimBST(root->left, L, R); root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
return root;
}
};

LeetCode 669. Trim a Binary Search Tree修剪二叉搜索树 (C++)的更多相关文章

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

  2. [CareerCup] 4.5 Validate Binary Search Tree 验证二叉搜索树

    4.5 Implement a function to check if a binary tree is a binary search tree. LeetCode上的原题,请参见我之前的博客Va ...

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

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

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

  6. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  7. [LeetCode] Binary Search Tree Iterator 二叉搜索树迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  8. [LeetCode] Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  9. [LeetCode] Validate Binary Search Tree 验证二叉搜索树

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

随机推荐

  1. Vue 变异方法Push的留言板实例

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. UAC简介

    用户帐户控制 (User Account Control) 是Windows Vista(及更高版本操作系统)中一组新的基础结构技术,可以帮助阻止恶意程序(有时也称为“恶意软件”)损坏系统,同时也可以 ...

  3. 海边拾贝-F-第三方项目

    第三方网站,不定期更新: 陈浩个人博客: https://coolshell.cn/ 阮一峰个人博客:http://www.ruanyifeng.com/blog/2015/02/make.html ...

  4. vuex 源码分析(六) 辅助函数 详解

    对于state.getter.mutation.action来说,如果每次使用的时候都用this.$store.state.this.$store.getter等引用,会比较麻烦,代码也重复和冗余,我 ...

  5. c#的文本格式化形式展示

    假设你使用的是新版本的的c#语法 c#的格式化形式有如下几种 string text = "Hello World!"; Console.WriteLine("Hello ...

  6. Reids Lua 模糊查询所有key 及 相对应的集合总数

    Redis 使用 Lua 模糊查询所有key 及 相对应的集合总数 .Net 4.5.1 需要引入:    StackExchange.Redis  (测试用的 1.2.4.0) 方法一: 优点:原子 ...

  7. 使用IDEA的Git插件上传项目教程

    如何使用IDEA的Git插件上传项目 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载 二.注册码云账号 搜索gitee码云插件并安装

  8. win10 net framework 3.5提示错误代码0x800f081f

    重装了win10系统,碰到以下几个问题 1.安装本地iis -启动或关闭windonws功能- 安装net framework 3.5的时候 提示错误代码0x800f081f 2.安装SqlServe ...

  9. easyui高级控件

    开发模式 1. 美工(ui工程师:出一个项目模型) java工程师:将原有的html转成jsp,动态展示数据 缺点: 客户需要调节前端的展示效果 解决:由美工去重新排版,重新选色.2.前后端分离 美工 ...

  10. Python【day 11】闭包

    闭包 1.闭包的概念: 嵌套函数中,父级函数的变量,在子集函数中用到了(访问.修改.返回),那么这个变量就被保护起来了 只有自己可以修改,父级函数()()就是闭包函数 2.闭包的特点: 1.常驻内存 ...