当前位置:网站首页>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);
}
}
}边栏推荐
- MySQL interview - the response of executing SQL is relatively slow, and the troubleshooting ideas.
- 1464. 数组中两元素的最大乘积
- TCP的那点玩意儿
- Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely
- Atlassian Confluence 远程代码执行漏洞(CVE-2022-26134漏洞分析与防护
- 力扣 272. 最接近的二叉搜索树值 II 递归
- allgero报错:Program has encountered a problem and must exit. The design will be saved as a .SAV file
- 【莫比乌斯反演】
- 27. remove elements
- ffmpeg+SDL2实现音频播放
猜你喜欢

产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具

Share the process requirements for single-layer flexible circuit board

Anaconda navigator启动慢的一个解决方法

FM信号、调制信号和载波

新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍

50 pieces of professional knowledge of Product Manager (IV) - from problem to ability improvement: amdgf model tool

剑指offer刷题(中等等级)

Sword finger offer II 027 Palindrome linked list

Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely

Ubuntu18下登录mysql 5.7设置root密码
随机推荐
云计算考试版本1.0
消息中间件之ActiveMQ的基本使用
Atlas conference vulnerability analysis collection
allgero报错:Program has encountered a problem and must exit. The design will be saved as a .SAV file
Machine learning notes linear regression of time series
【论文学习】《VQMIVC》
This article uses pytorch to build Gan model!
电子学:第010课——实验 8:继电振荡器
电子学:第013课——实验 14:可穿戴的脉冲发光体
现在通过开户经理发的开户链接股票开户安全吗?
Pycharm的奇葩设定:取消注释后立马复制会带上#
Microsoft Office Word 远程命令执行漏洞(CVE-2022-30190)分析与利用
@The difference between resource and @autowired annotation, why recommend @resource?
Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely
电子学:第010课——实验 9:时间与电容器
共话云原生数据库的未来
Anaconda based module installation and precautions
三台西门子消防主机FC18配套CAN光端机进行光纤冗余环网组网测试
剑指offer刷题(中等等级)
Share the process requirements for single-layer flexible circuit board
