[LeetCode&Python] Problem 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.
Example 1:
- Input:
- 1
- / \
- 0 2
- L = 1
- R = 2
- Output:
- 1
- \
- 2
Example 2:
- Input:
- 3
- / \
- 0 4
- \
- 2
- /
- 1
- L = 1
- R = 3
- Output:
- 3
- /
- 2
- /
- 1
- # 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, L, R):
- """
- :type root: TreeNode
- :type L: int
- :type R: int
- :rtype: TreeNode
- """
- if root==None:
- return root
- if R<root.val:
- return self.trimBST(root.left,L,R)
- if L>root.val:
- return self.trimBST(root.right,L,R)
- root.left=self.trimBST(root.left,L,R)
- root.right=self.trimBST(root.right,L,R)
- return root
[LeetCode&Python] Problem 669. Trim a Binary Search Tree的更多相关文章
- 【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 ...
- 【LeetCode】669. Trim a Binary Search Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcod ...
- 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 t ...
- [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 ...
- 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 t ...
- 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 ...
随机推荐
- 编写自己的代码库(javascript常用实例的实现与封装)[转]
1.前言 因为公司最近项目比较忙,没那么多空余的事件写文章了,所以这篇文章晚了几天发布.但是这也没什么关系,不过该来的,总是会来的.好了,其他的不多说的,大家在开发的时候应该知道,有很多常见的实例操作 ...
- URAL 1501 Sense of Beauty
URAL 1501 思路: dp+记忆化搜索 状态:dp[i][j]表示选取第一堆前i个和第二堆前j的状态:0:0多1个 1:0和1相等 2:1 ...
- Codeforces 559B - Equivalent Strings
559B - Equivalent Strings 思路:字符串处理,分治 不要用substr(),会超时 AC代码: #include<bits/stdc++.h> #include&l ...
- C#中类的序列化和反序列化
说明:本文演示将类序列化后写入记事本并从记事本读取反序列化为对象1.首先创建一个类,同时类必须标识为Serializable,如下: [Serializable] public class Regio ...
- IIS中发布后出现Could not load file or assembly'System.Data.SQLite.dll' or one of its depedencies
[问题]在我本机的开发环境c#连接sqlite3没有问题,可是release版本移植到其他的机器就提示Could not load file or assembly'System.Data.SQLit ...
- English trip -- VC(情景课) 7 A Shopping 购物
Words The clothes place a dress 长裙 short skirt 短裙 pants 裤子 / trousers 长裤 / shorts 短裤 a shir ...
- (转)nginx做转发时,带'_'的header内容丢失
原本在测试环境测试通过的APP,今天准备切到线上环境做最后测试,结果发现了错误.查看日志发现是APP端发送的http请求中的header内容丢失了.那么代码没有改动,怎么平白无故会丢失头信息? 于是想 ...
- 49 DOM(2)
一.value属性: input ,select 标签 ,textarea 标签中有value属性, 获取他们属性值的方法,先获取该元素ele,然后ele.value得到value值. <!DO ...
- Python基础--列表、元组
一.什么是列表.元组 序列是Python中最基本的数据结构.序列中的每个元素都分配一个数字 - 它的位置,或索引,第一个索引是0,第二个索引是1,依此类推. Python有6个序列的内置类型,但最常见 ...
- 操作系统错误 5:"5(拒绝访问。)
------------------------------ 无法打开物理文件 "G:/QGJX.mdf".操作系统错误 5:"5(拒绝访问.)". (Micr ...