当前位置:网站首页>Force buckle 272 Closest binary search tree value II recursion
Force buckle 272 Closest binary search tree value II recursion
2022-06-25 08:00:00 【Empress Yu】
Power button 272. The closest binary search tree value II
Given the root of a binary search tree
root、 A target valuetargetAnd an integerk, return BST Closest to target inkIt's worth . You can press In any order Return to the answer .subject Guarantee In this binary search tree, there is only one kind of k Sets of values are the closest
targetExample 1:
Input : root = [4,2,5,1,3], The target = 3.714286, And k = 2 Output : [4,3]Example 2:
Input : root = [1], target = 0.000000, k = 1 Output : [1]Tips :
- The total number of nodes in the binary tree is
n1 <= k <= n <= 10^40 <= Node.val <= 10^9-109 <= target <= 10^9source : Power button (LeetCode)
link :https://leetcode.cn/problems/closest-binary-search-tree-value-ii
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The result of doing the question
adopt , Just pure tree recursion
Method 1-1: Merging recursion
Because both of them are recursive, let's divide them into small points
Merge type means to push directly from the child result to the father , Merge the results of parent-child merging
1. Before calculating the left subtree n individual , Before calculating the right subtree n individual , Add in the middle 2n+1 individual , The part beyond , By deleting the far left and right nodes
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);
}
}Method 1-2: Sliding window recursion
Keep... In the window k There is a feasible solution
1. The order in a binary search tree is equivalent to traversing an ordered array
2. front k Direct access
3. If exceeded k individual , Check if the larger value is closer to , If not, just quit , If yes, remove the first one and add a current value
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);
}
}
}边栏推荐
- PCB board design - automatic layout 2021-10-15
- c#磁盘驱动器及文件夹还有文件类的操作
- TCP与UDP
- 【补题】2021牛客暑期多校训练营6-n
- Debugging mipi-dsi screen based on stm32mp157
- Atlas conference vulnerability analysis collection
- 电子学:第008课——实验 6:非常简单的开关
- Tips 𞓜 how to clean PCB boards 2021-10-22
- allgero报错:Program has encountered a problem and must exit. The design will be saved as a .SAV file
- Tips on how to design soft and hard composite boards ~ 22021/11/22
猜你喜欢

Take you through the normalization flow of GaN

Matlab代码格式一键美化神器

How to select lead-free and lead-free tin spraying for PCB? 2021-11-16

To understand the difference between Gram-positive and Gram-negative bacteria and the difference in pathogenicity

深度学习系列45:图像恢复综述

Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system

电子学:第014课——实验 15:防入侵报警器(第一部分)

Invalid Navicat scheduled task

使用报文和波形记录分析仪RoyalScope的帧统计功能排查CAN总线偶发性故障

Anaconda navigator启动慢的一个解决方法
随机推荐
Can bus working condition and signal quality "physical examination"
將數據導入到MATLAB
电子学:第010课——实验 9:时间与电容器
50. pow (x, n) - fast power
基于Anaconda的模块安装与注意事项
将数据导入到MATLAB
Debugging mipi-dsi screen based on stm32mp157
@Resource和@Autowired注解的不同,为什么推荐@Resource?
Neural network and deep learning-3-simple example of machine learning pytorch
What are the problems with traditional IO? Why is zero copy introduced?
Pycharm的奇葩设定:取消注释后立马复制会带上#
How to use ad wiring for PCB design?
[deep learning lightweight backbone] 2022 edgevits CVPR
Looking for b-end product manager after years? I almost ruined myself
Electronics: Lesson 010 - Experiment 8: relay oscillator
2021ICPC网络赛第一场
MySQL简单权限管理
Ubuntu18下登录mysql 5.7设置root密码
不怕百战失利,就怕灰心丧气
How much do you know about electronic components on PCB?
