Leetcode669.Trim a Binary Search Tree修建二叉树
给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。
错误的答案:
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(root == NULL)
return NULL;
if(root ->val < L)
{
root = root ->right;
trimBST(root, L, R);
}
else if(root ->val > R)
{
root = root ->left;
trimBST(root, L, R);
}
else
{
root ->left = trimBST(root ->left, L, R);
root ->right = trimBST(root ->right, L, R);
}
return root;
}
};
正确的答案:
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if(root == NULL)
return NULL;
if(root ->val < L)
{
root = trimBST(root ->right, L, R);
}
else if(root ->val > R)
{
root = trimBST(root ->left, L, R);
}
else
{
root ->left = trimBST(root ->left, L, R);
root ->right = trimBST(root ->right, L, R);
}
return root;
}
};
Leetcode669.Trim a Binary Search Tree修建二叉树的更多相关文章
- LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)
669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...
- 【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 ...
- [Swift]LeetCode669. 修剪二叉搜索树 | 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] 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 that a ...
- LeetCode - 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 ...
- 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 ...
随机推荐
- Java英语面试题(核心知识篇)
Java英语面试题(核心知识篇) Question: What is transient variable?Answer: Transient variable can't be serialize. ...
- 杂项-公司:AT&T
ylbtech-杂项-公司:AT&T AT&T公司(英语:AT&T Inc.,原为American Telephone & Telegraph的缩写,也是中文译名美国电 ...
- Acer电脑【no bootable device】引导修复
时隔一年,又遇上了我的电脑出现 No Bootable Device 的开机提示,进不了系统. 那么怎么办呢? // 本文修复方式仅针对宏碁电脑且分区表为MBR的情况.按如下方法引导并未 ...
- jsp页面判断当前请求的host
需要引入<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %> ...
- 关于Collection接口和Map
Iterable才是Collection的父接口.不是Iterator. Map,SortedMap属于接口类型,不可以new的方式创建对象. HashMap基于哈希表实现Map接口的类,并允许nul ...
- 关于不同数据库的连接配置(MySql和Oracle)
mysql: driverClass=com.mysql.jdbc.Driver #在和mysql传递数据的过程中,使用unicode编码格式,并且字符集设置为utf-8,插入数据库防止中文乱码 ur ...
- Python学习day42-数据库的基本操作(1)
figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...
- 11-4-while和dowhile
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- vue @click传字符串
参考: https://www.cnblogs.com/springlight/p/5782637.html 关键:使用转译字符 \ 来转译引号 方法一. 直接传递: var tem = " ...
- PKU 百炼OJ 奖学金
http://bailian.openjudge.cn/ss2017/A/ #include<iostream> #include <cmath> #include <m ...