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 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的值位于L和R之间,则递归修剪其左右子树,返回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 解题报告的更多相关文章
- 【LeetCode】669. Trim a Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 【LeetCode】99. Recover Binary Search Tree 解题报告(Python)
[LeetCode]99. Recover Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/p ...
- [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 ...
- 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 ...
- 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 ...
- 【LeetCode】98. Validate Binary Search Tree 解题报告(Python & C++ & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 BST的中序遍历是有序的 日期 题目地址:ht ...
- [leetcode]669. Trim a Binary Search Tree寻找范围内的二叉搜索树
根据BST的特点,如果小于L就判断右子树,如果大于R就判断左子树 递归地建立树 public TreeNode trimBST(TreeNode root, int L, int R) { if (r ...
- 【Leetcode_easy】669. Trim a Binary Search Tree
problem 669. Trim a Binary Search Tree 参考 1. Leetcode_easy_669. Trim a Binary Search Tree; 完
- 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 ...
随机推荐
- oracle 回收表空间的数据文件大小
查看表空间的使用情况: " "used MB",b.bytes "free MB", ,) "percent_used" from ...
- hdoj:2035
#include <iostream> using namespace std; int main() { long a, b; && b != ) { long resu ...
- Linux(C/C++)下的文件操作open、fopen与freopen
open是linux下的底层系统调用函数, fopen与freopen c/c++下的标准I/O库函数,带输入/输出缓冲. linxu下的fopen是open的封装函数,fopen最终还是要调用底层的 ...
- linux:rsync + inotifywait 实现【准实时】同步
直接上脚本 #!/bin/bash export PATH=./sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:$PATH src=/tmp1 #dest=ro ...
- laravel 在linux环境下解决.htaccess无效和去除index.php
LoadModule rewrite_module modules/mod_rewrite.so (去掉前面的#注释) AllowOverride All (根目录的配置下,确保设置成All) < ...
- CentOS使用virt-what知道虚拟机的虚拟化技术
通常拿到一台vps,提供商可能不会告诉我们具体的虚拟化技术,对于CentOS的系统的vm,可以使用virt-what来知道. 如果提示virt-what命令找不到,则需要安装一下 yum instal ...
- Django之Web框架本质及第一个Django实例
Web框架本质 我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 半成品自定义web框架 impor ...
- Intersection Observer API 可以让你知道被观察元素何时进入或退出浏览器的视口
google 文档 https://developers.google.cn/web/updates/2016/04/intersectionobserver MDN 文档 https://devel ...
- juqery 判断所有input 不能为空 判断只能为数字 判断身份证号:18位和15位 判断是否银行卡号
//jq 判断某字符串是否含有特殊符号 function CheckNum() { //定义数组保存特殊字符 var AllNumIsSame = new Array("’", & ...
- js中if语句的几种优化代码写法
UglifyJS是一个对javascript进行压缩和美化的工具,在它的文档说明中,我看到了几种关于if语句优化的方法. 一.使用常见的三元操作符 复制代码 代码如下: if (foo) bar(); ...