题目要求

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.

题目分析及思路

题目给出一棵二叉搜索树和一个上下界,要求对树剪枝,使得结果树中的所有结点的值都在给定的区间内。二叉搜索树的定义为:该树可为空;不空的话,若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值,若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值。根据该定义,我们可以使用递归的方法,当root的值位于LR之间,则递归修剪其左右子树,返回root。当root的值小于L,则其左子树的值都小于L,抛弃左子树,返回修剪过的右子树。当root的值大于R,则其右子树的值都大于R,抛弃右子树,返回修剪过的左子树。

python代码

# Definition for a binary tree node.

# class TreeNode:

#     def __init__(self, x):

#         self.val = x

#         self.left = None

#         self.right = None

class Solution:

def trimBST(self, root: TreeNode, L: int, R: int) -> TreeNode:

if not root:

return

if root.val < L:

return self.trimBST(root.right, L, R)

elif root.val > R:

return self.trimBST(root.left, L, R)

else:

root.left = self.trimBST(root.left, L, R)

root.right = self.trimBST(root.right, L, R)

return root

LeetCode 669 Trim a Binary Search Tree 解题报告的更多相关文章

  1. 【LeetCode】669. Trim a Binary Search Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...

  2. 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)

    [LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...

  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 that a ...

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

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

    题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...

  6. 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...

  7. [leetcode]669. Trim a Binary Search Tree寻找范围内的二叉搜索树

    根据BST的特点,如果小于L就判断右子树,如果大于R就判断左子树 递归地建立树 public TreeNode trimBST(TreeNode root, int L, int R) { if (r ...

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

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

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

随机推荐

  1. [转]PowerDesigner大小写转换

    原文地址:https://blog.csdn.net/fzqlife/article/details/72769959?utm_source=blogxgwz7 在菜单栏找到:Tools-->E ...

  2. MySQL视图小例子

    场景: 某查询接口 查询sql语句已确定,用该sql语句去查 表 t_strategy_stock 中的数据,但是 表t_strategy_stock 的字段名称和 sql 语句中写死的名称不同. 需 ...

  3. jquery 动画总结(主要指效果函数)

    动画无非两类:帧动画frame和变形动画tween,以及3d动画.不论web还是安卓苹果app,动画原理都是这些. web app 动画实现的途径,无非这几种:1 gif动画---这就是帧动画,把若干 ...

  4. 面向对象方法的重载(overloading)和覆盖(overriding)。

      在有些JAVA书籍中将overriding称为重载,overloading称为过载. Overloading在一个类中可以定义多个同名方法,各个方法的参数表一定不同.但修饰词可能相同,返回值也可能 ...

  5. Unity3D中Layers和LayerMask解析

    Unity中是用int32来表示32个Layer层.int32表示二进制一共有32位(0—31).在Unity中可编辑的Layer如下图所示:   在Unity中每个GameObject都有Layer ...

  6. 【SpringCloud微服务实战学习系列】服务治理Spring Cloud Eureka

    Spring Cloud Eureka是Spring Cloud Netflix微服务中的一部分,它基于NetFlix Sureka做了二次封装,主要负责完成微服务架构中的服务治理功能. 一.服务治理 ...

  7. web站点健康检测和告警小脚本

    #!/bin/sh web01="http://172.18.52.xx:8080/web/api/getTime" web02="http://172.18.52.xx ...

  8. 高斯混合模型 GMM

    本文将涉及到用 EM 算法来求解 GMM 模型,文中会涉及几个统计学的概念,这里先罗列出来: 方差:用来描述数据的离散或波动程度. \[var(X) =  \frac{\sum_{i=1}^N( X_ ...

  9. sometimes we should use "disable fork" instead of "disable block_name"

    A disable named block statement stops the execution of all blocks with that same name in all threads ...

  10. yii2优化 - 开启 Schema 缓存

    开启 Schema 缓存 Schema 缓存是一个特殊的缓存功能,每当你使用活动记录时应该要开启这个缓存功能.如你所知, 活动记录能智能检测数据库对象的集合(例如列名.列类型.约束)而不需要手动地描述 ...