题目要求

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. 用opencv抽取视频的帧并保存为连续的图片

    转自http://blog.csdn.net/timidsmile/article/details/8283319 #include"stdafx.h" #include < ...

  2. linux 修改文件类型

    :Unix及类Unix系统里,每行结尾只有换行“\n”,Windows系统里面,每行结尾是换行+回车“\n\r”. 利用Linux下的vim编辑器,可以方便的在dos文件.unix文件之间进行切换,且 ...

  3. linux memcached

    依赖库 yum install libevent libevent-deve 云安装 yum install memcached 源代码安装 wget http://memcached.org/lat ...

  4. 汉字 Unicode 编码范围

    字符集 字数 Unicode 编码 基本汉字 20902字 4E00-9FA5 基本汉字补充 38字 9FA6-9FCB 扩展A 6582字 3400-4DB5 扩展B 42711字 20000-2A ...

  5. [IR] Arithmetic Coding

    Statistical methods的除了huffman外的另一种常见压缩方式. Huffman coding的非连续数值特性成为了无法达到香农极限的先天无法弥补的缺陷,但Arithmetic co ...

  6. 13组合模式Composite

    一.什么是组合模式 Composite模式也叫组合模式,是构造型的设 计模式之一.通过递归手段来构造树形的对象结 构,并可以通过一个对象来访问整个对象树. 二.组合模式的结构 三.组合模式的角色和职责 ...

  7. 【Docker】文件拷贝

    从容器复制到主机sudo docker cp containerID:container_path host_path docker cp 5c6ce895b979:/root/LearnPaddle ...

  8. 深入Java内存模型之阅读理解(1)

    Java内存模型的抽象 在java中,所有实例域.静态域和数组元素存储在堆内存中,堆内存在线程之间共享(本文使用“共享变量”这个术语代指实例域,静态域和数组元素).局部变量(Local variabl ...

  9. spring框架的ioc

       spring框架,主要思想可以用spring容器来理解 aop是一种对oop进行补充的软件设计思想,将和核心功能不相关代码抽象出来,由其他类完成.比如Singer类,我们可以定义一个Singer ...

  10. MySQL 之 表操作

    一 什么是表 表(TABLE) 是一种结构化的文件,可用来存储某种特定类型的数据.表中的一条记录有对应的标题,标题 称之为 表的字段. 二 创建表 1 2 3 4 5 CREATE TABLE 表名( ...