题目要求

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. 【Java】加载驱动方法

    1.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 2. DriverManager.register ...

  2. pom.xml文件详解

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  3. 【iCore4 双核心板_ARM】例程二十:LWIP_TCP_CLIENT实验——以太网数据传输

    实验现象: 核心代码: int main(void) { system_clock.initialize(); //ϵͳʱÖÓ³õʼ»¯ led.initialize(); //LED³õʼ ...

  4. hdoj:2037

    #include <iostream> using namespace std; struct Time { int start; int end; }; Time times[]; ]; ...

  5. .net core实现跨域

    什么是跨域在前面已经讲解过了,这里便不再讲解,直接上代码. 一.后台API接口 用.net core创建一个Web API项目负责给前端界面提供数据. 二.前端界面 建立两个MVC项目,模拟不同的ip ...

  6. 关于Unity中NGUI的Tab商城、Scrollview和打字机效果的实现

    Tab商城实例 UIToggle 和 UIToggledObjects+ Box Collider(实现商城功能必备) 1.创建两个个UI Sprite,Sprite1和Sprite2 2.给Spri ...

  7. (原)关于获取ffmpeg解析rtsp流sdp中带有sps,pps的情况

     转载请注明出处:http://www.cnblogs.com/lihaiping/p/6612511.html 今天同事准备在android下使用ffmpeg来获取rtsp流,问我如何获取获取sps ...

  8. [SLAM] 03. ORB-SLAM2

    一年后再读SLAM~ 行业有了不少工程实践方面的突破 一.链接:https://www.zhihu.com/question/53571648/answer/176732257 目前来说,受到业界肯定 ...

  9. [Artoolkit] Marker of nftSimple

    重点看:markers.dat 的解析原理 1. int main(int argc, char** argv) { ]; const char *cparam_name = "Data2/ ...

  10. ElasticSearch6(二)-- Java API连接es

    此ElasticSearch系列基于最新版的6.2.4版本. 一.pom.xml依赖 <dependencies> <dependency> <groupId>ju ...