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 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
代码:
二叉查找树(英语:Binary Search Tree),也称二叉搜索树、有序二叉树(英语:ordered binary tree),排序二叉树(英语:sorted binary tree),是指一棵空树或者具有下列性质的二叉树:
- 若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
- 若任意节点的右子树不空,则右子树上所有节点的值均大于它的根节点的值;
- 任意节点的左、右子树也分别为二叉查找树;
- 没有键值相等的节点。
使用递归:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(!root)
return root;
if(root->val>=L && root->val<=R) {
root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
}
else if(root->val > R) {
root->left = trimBST(root->left, L, R);
root=root->left;
}
else {
root->right = trimBST(root->right, L, R);
root=root->right;
}
return root;
}
};
LeetCode: 669 Trim a Binary Search Tree(easy)的更多相关文章
- [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修剪二叉搜索树 (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_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&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 a ...
- 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 ...
随机推荐
- new 和 make 均是用于分配内存
the-way-to-go_ZH_CN/06.5.md at master · Unknwon/the-way-to-go_ZH_CN https://github.com/Unknwon/the-w ...
- var foo = "11"+2+"1"; console.log(foo); //1121 好多文章答案写错了,我发下给初学的朋友看到,以免一开始就学错了
体会加一个字符串'1' 和 减去一个字符串'1'的不同 var foo = "11"+2-"1"; console.log(foo); //111 consol ...
- oracle 11g ocr 冗余配置
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/royjj/article/details/30506343 oracle 11g ocr 冗余 ...
- 【ELK】ELK5.3搭建过程遇到的问题
elasticsearch 5.3 安装过程中遇到了一些问题,这里简单记录一下 . 问题一:警告提示 [2016-11-06T16:27:21,712][WARN ][o.e.b.JNANatives ...
- perl之创建临时文件夹遇到同名文件该咋办
当你在目录下进行一系列操作时,若要创建许多文件或者修改文件,可能会遇到许多麻烦的事.所以呢,新建一个文件夹,然后在这个文件夹下新建文件或者修改文件.假设,你的代码要在一个目录下新建一个文件夹,名为Tm ...
- nginx.config配置文件模板
#user nobody;worker_processes 1; #error_log logs/error.log;#error_log logs/error.log notice;#error_l ...
- Composer基础应用1
先唠叨唠叨一些琐碎的事.本人最早从事.Net开发,后来处于好奇慢慢转到了php,因为.net从一早就使用了命名空间(反正从我使用就存在这玩意了),所以在转php时很自然的就使用了命名空间,但是在使用过 ...
- hdu 6006
HDU - 6006 Engineer Assignment 我参考了这份题解. 贴上我比较拙的代码,留念一下. /** * 想到状态压缩的dp问题就解决了一半. */ #include <st ...
- September Challenge 2017
Little Chef and Sums 分析:水题,去维护一下前缀和以及后缀和就好,注意long long #include "iostream" #include " ...
- Django 模板 自定义context_processors
Django版本 1.8.4 为什么要自定义context_processors 在做博客的过程中,在浏览排行,评论排行,标签,文章归档,友情链接等内容每一个显示页面都是要显示的.如果在每一个view ...