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 ...
随机推荐
- Asp.Net 导入Excel自动获取表名
public static DataSet ReadExcel(string Path, string fileType) { //服务器需要安装驱动 //http://download.micros ...
- React Native - FlexBox弹性盒模型
FlexBox布局 1. 什么是FlexBox布局? 弹性盒模型(The Flexible Box Module),又叫FlexBox,意为"弹性布局",旨在通过弹性的方式来对 ...
- Oracle Enterprise Linux 6.4 下配置vncserver
① 安装vncserveryum install tigervnc-server ② 配置/etc/sysconfig/vncservers 配置参数 # VNCSERVERS="2 ...
- PHP最全笔记(五)(值得收藏,不时翻看一下)
// 删除 方法1:将其值设置为空字符串 setcookie('user[name]', ''); 方法2:将目标cookie设为“已过期”状态. //将cookie的生存时间设置为过期,则生存期限与 ...
- [OpenCV] Samples 17: Floodfill
一种聚类方式,代码解析 #!/usr/bin/env python ''' Floodfill sample. Usage: floodfill.py [<image>] Click on ...
- jenkins 部署问题
1. 怀疑 jenkins 没有拉到最新的包解决:清除 jenkins 工作空间 2. jenkins 自动构建时, start.sh 脚本已经执行成功,但是应用总是启动后自动退出了进程没有起来,从 ...
- 为何谷歌围棋AI AlphaGo可能会把李世石击溃
/* 版权声明:可以随意转载,转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 谷歌DeepMind开发的人工智能围棋程序AlphaGo以5:0的压倒性优势击败了欧洲围棋冠军.专业二 ...
- No suitable servers found (`serverselectiontryonce` set): [Failed connecting to '115.28.161.44:27017': Connection timed out] php mongodb 异常
我 php mongodb 拓展使用的是 MongoDB driver 今天查询数据的时候 偶尔会提示, No suitable servers found (`serverselectiontry ...
- Android开发训练之第五章第七节——Transmitting Network Data Using Volley
Transmitting Network Data Using Volley GET STARTED DEPENDENCIES AND PREREQUISITES Android 1.6 (API L ...
- angular 4 和django 1.11.1 前后端交互 总结
首先 angular4 和django 1.11.1交互 有跨域问题 所以先关闭cors 和csrf验证 一.解决跨域问题 cors github django-cors-headers 1)安装co ...