当前位置:网站首页>力扣 272. 最接近的二叉搜索树值 II 递归
力扣 272. 最接近的二叉搜索树值 II 递归
2022-06-25 06:43:00 【钰娘娘】
力扣 272. 最接近的二叉搜索树值 II
给定二叉搜索树的根
root、一个目标值target和一个整数k,返回BST中最接近目标的k个值。你可以按 任意顺序 返回答案。题目 保证 该二叉搜索树中只会存在一种 k 个值集合最接近
target示例 1:
输入: root = [4,2,5,1,3],目标值 = 3.714286,且 k = 2 输出: [4,3]示例 2:
输入: root = [1], target = 0.000000, k = 1 输出: [1]提示:
- 二叉树的节点总数为
n1 <= k <= n <= 10^40 <= Node.val <= 10^9-109 <= target <= 10^9来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/closest-binary-search-tree-value-ii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
做题结果
通过,就纯粹的树的递归
方法1-1:归并型的递归
因为两个都是递归就分一下小点
归并型的意思就是直接从子结果推到父,合并父子合并的结果
1. 计算出左子树前n个,计算右子树前n个,加上中间一共2n+1个,超出的部分,通过删除左右两侧较远的节点实现
class Solution {
public List<Integer> closestKValues(TreeNode root, double target, int k) {
Deque<Integer> ans = new LinkedList<>();
if(root == null) return new ArrayList<>();
List<Integer> left = closestKValues(root.left,target,k);
List<Integer> right = closestKValues(root.right,target,k);
ans.addAll(left);
ans.add(root.val);
ans.addAll(right);
if(ans.size()<=k){
return new ArrayList<>(ans);
}
while (ans.size()>k){
double d1 = Math.abs(ans.getFirst()-target);
double d2 = Math.abs(ans.getLast()-target);
if(d1>d2){
ans.removeFirst();
}else{
ans.removeLast();
}
}
return new ArrayList<>(ans);
}
}方法1-2:滑窗型的递归
保持窗口内有k个可行解
1. 二叉搜索树中序相当于顺序遍历有序数组
2. 前面k个直接取
3. 如果超过k个,检查更大值是不是更接近,不是就直接退出,是就移除第一个加一个当前值
class Solution {
List<Integer> result = new LinkedList<>();
public List<Integer> closestKValues(TreeNode root, double target, int k) {
dfs(root,target,k);
return result;
}
public void dfs(TreeNode root, double target, int k) {
if (root == null) return ;
dfs(root.left,target,k);
if (result.size() < k || Math.abs(result.get(0) - target) > Math.abs(root.val - target)) {
if(result.size()==k) result.remove(0);
result.add(root.val);
dfs(root.right,target,k);
}
}
}边栏推荐
- 50. pow (x, n) - fast power
- Kinsing双平台挖矿家族病毒分析
- 机器学习笔记 - 时间序列的线性回归
- 取消word文档中某些页面的页眉
- Pcb|about FPC reinforcement type
- Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system
- Cifar-10 dataset application: quick start data enhancement method mixup significantly improves image recognition accuracy
- Technology blog | how to communicate using SSE
- 【日常训练】207. 课程表
- Five causes of PCB board deformation and six solutions 2021-10-08
猜你喜欢
![[deep learning lightweight backbone] 2022 edgevits CVPR](/img/13/139d28621403020e3475a30f6148f8.png)
[deep learning lightweight backbone] 2022 edgevits CVPR

Sword finger offer II 027 Palindrome linked list

基于RBAC 的SAAS系统权限设计

OAuth 2.0 one click login

【深度学习 轻量型backbone】2022 EdgeViTs CVPR

一次弄清楚 Handler 可能导致的内存泄漏和解决办法

Take you through the normalization flow of GaN

Six causes of PCB disconnection 2021-10-20

Terms and concepts related to authority and authentication system

Invalid Navicat scheduled task
随机推荐
Mining microbial dark matter -- a new idea
静态网页服务器
How much do you know about electronic components on PCB?
socket问题记录
LeetCode_哈希表_中等_454.四数相加 II
协议和服务的区别?
Six causes of PCB disconnection 2021-10-20
剑指 Offer II 027. 回文链表
ffmpeg+SDL2实现音频播放
Take you through the normalization flow of GaN
417-二叉树的层序遍历1(102. 二叉树的层序遍历、107.二叉树的层次遍历 II、199.二叉树的右视图、637.二叉树的层平均值)
Find out what informatization is, and let enterprises embark on the right path of transformation and upgrading
"Spatial transformation" significantly improves the quality of ground point extraction of cliff point cloud
Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely
挖掘微生物暗物质——新思路
[little knowledge] PCB proofing process
【补题】2021牛客暑期多校训练营1-3
云计算考试版本1.0
剑指offer刷题(中等等级)
微信小程序入门记录
