leetcode算法:Trim a Binar 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 这道题描述的需求是:
给我们一个二叉排序树 最小数l 和最大数r
我们需要做的是对这棵树进行裁剪,让树里所有的节点值都满足在l和r之间 思想:
二叉排序树的特点是:对于任意一个节点,左子树任意节点数值都比跟节点小,右子树任意节点数值都比根大
所以考虑,对于任意一个节点,
如果值比l还小,那就应该抛弃这个根,去右子树寻找新的根
如果值比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, L, R):
"""
:type root: TreeNode
:type L: int
:type R: int
:rtype: TreeNode
"""
if root is None:
return None
if root.val >R:
return self.trimBST(root.left ,L,R)
if root.val<L:
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 if __name__ == '__main__':
s = Solution() root = TreeNode(1)
root.left = TreeNode(0)
root.right = TreeNode(2) print(root.val,root.left.val,root.right.val) root = s.trimBST(root,1,2) print(root, root.left, root.right)
leetcode算法:Trim a Binar Search Tree的更多相关文章
- [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 解题报告
题目要求 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(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]669. Trim a Binary Search Tree寻找范围内的二叉搜索树
根据BST的特点,如果小于L就判断右子树,如果大于R就判断左子树 递归地建立树 public TreeNode trimBST(TreeNode root, int L, int R) { if (r ...
- LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)
669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- 【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 ...
随机推荐
- QT 创建一个具有复选功能的下拉列表控件
最近研究了好多东西,前两天突然想做一个具有复选功能的下拉列表框.然后在网上"学习"了很久之后,终于发现了一个可以用的,特地发出来记录一下. 一.第一步肯定是先创建一个PROJECT ...
- Xshell提示缺失mfc110.dll
xshell 应用程序无法正常启动0xc000007b 下载 DirectX修复工具_3.3 Xshell 缺少 mfc110.dll https://www.microsoft.co ...
- 安装Oracle11g的依赖包
binutils-2.17.50.0.6 compat-libstdc++-33-3.2.3 elfutils-libelf-0.125 elfutils-libelf-devel-0.125 elf ...
- Shiro【授权、整合Spirng、Shiro过滤器】
前言 本文主要讲解的知识点有以下: Shiro授权的方式简单介绍 与Spring整合 初始Shiro过滤器 一.Shiro授权 上一篇我们已经讲解了Shiro的认证相关的知识了,现在我们来弄Shiro ...
- C语言第三次博客作业---单层循环结构
一.PTA实验作业 题目1 1.实验代码 int N,i; //N为用户数,i记录循环变量 double height; //height放身高 char sex; //sex放性别F为女,M为男 s ...
- 代码重构--switch的惊恐现身
switch作为条件判断(分支结构)中的一种方式,以至于我们对于它使用的频率处于较高水平的水平线上,为此我们应该使用Extra method来对这类判断条件进行抽取,另外从我自身而言,我发现我以前常常 ...
- C++环境搭建与atom编译器编译C++
Windows下安装 方法一--VS: 使用windows开发神器visio studio.这种方法比较简单,直接下载一个最新的vs安装就行.不单单是C++,C.C#.VB等都可以开发. 方法二--只 ...
- .Net的垃圾回收机制(GC)之拙见——托管类型的垃圾回收
各种语言的垃圾回收在IT界噪的沸沸扬扬,有极大的优化同时也有瓶颈. 而在.Net中的垃圾回收机制又是怎样的呢? 众所知周,.Net中的垃圾回收机制是由.Net Framework托管的,带给开发者最大 ...
- C#/AutoCAD 2018/ObjectArx/二次开发再说实体(六)
这些函数对大家很有用,如果想获取详细源代码请加云幽学院yunyou.ke.qq.com报名免费课程,如果想学习更系统.更全面的知识请报名收费课程,有大量开发案例共享. 1.获取模型空间中所有实体 #r ...
- hadoop集群简单搭建
分布式搭建 在ubuntu下创建hadoop用户组和用户 bigdata@master:~$sudo addgroup hadoop bigdata@master:~$sudo adduser --i ...