当前位置:网站首页>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);
}
}
}边栏推荐
- SCM Project Training
- WebSocket的理解以及应用场景
- allgero报错:Program has encountered a problem and must exit. The design will be saved as a .SAV file
- Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134 vulnerability analysis and protection
- php数组函数大全
- Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test
- MySQL简单权限管理
- c#搭建ftp服务器并实现文件上传和下载
- c#磁盘驱动器及文件夹还有文件类的操作
- What are the problems with traditional IO? Why is zero copy introduced?
猜你喜欢

Mining microbial dark matter -- a new idea

Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus

Apache CouchDB 代码执行漏洞(CVE-2022-24706 )批量POC
![洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)](/img/cb/046fe4b47898fd6db86edc8a267c34.png)
洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)

Technology blog | how to communicate using SSE

The fourth floor is originally the fourth floor. Let's have a look

时钟刻度盘的绘制

c#磁盘驱动器及文件夹还有文件类的操作

DNS协议及其DNS完整的查询过程

Tips on how to design soft and hard composite boards ~ 22021/11/22
随机推荐
三台西门子消防主机FC18配套CAN光端机进行光纤冗余环网组网测试
电子学:第014课——实验 15:防入侵报警器(第一部分)
How to use ad wiring for PCB design?
新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
C#中如何调整图像大小
力扣 272. 最接近的二叉搜索树值 II 递归
一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
深度学习系列45:图像恢复综述
c#ColorDialog更改文本颜色和FontDialog更改文本字体的使用示例
TCP的那点玩意儿
Linux上oracle和mysql的启动,关闭,重启
DNS协议及其DNS完整的查询过程
Force deduction 76 questions, minimum covering string
bat启动.NET Core
Technology blog | how to communicate using SSE
洛谷P1073 [NOIP2009 提高组] 最优贸易(分层图+最短路)
Cifar-10 dataset application: quick start data enhancement method mixup significantly improves image recognition accuracy
TCP与UDP
Opencv daily function structure analysis and shape descriptor (8) Fitline function fitting line
