[Locked] Closest Binary Search Tree Value & Closest Binary Search Tree Value II
Closest Binary Search Tree Value
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.
Note:
- Given target value is a floating point.
- You are guaranteed to have only one unique value in the BST that is closest to the target.
分析:
按照正常的搜索路径,直到搜索到叶节点,选出在这个路径上离target最近的值返回
代码:
- void dfs(TreeNode *cur, int &cv, double target) {
- if(!cur)
- return;
- double num = double(cur->val) - target;
- if(abs(num) < abs(double(cv) - target))
- cv = cur->val;
- if(num > )
- dfs(cur->left, cv, target);
- else
- dfs(cur->right, cv, target);
- return;
- }
Closest Binary Search Tree Value II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.
Note:
- Given target value is a floating point.
- You may assume k is always valid, that is: k ≤ total nodes.
- You are guaranteed to have only one unique set of k values in the BST that are closest to the target.
Follow up:
Assume that the BST is balanced, could you solve it in less than O(n) runtime (where n = total nodes)?
Hint:
- Consider implement these two helper functions:
getPredecessor(N)
, which returns the next smaller node to N.getSuccessor(N)
, which returns the next larger node to N.
- Try to assume that each node has a parent pointer, it makes the problem much easier.
- Without parent pointer we just need to keep track of the path from the root to the current node using a stack.
- You would need two stacks to track the path in finding predecessor and successor node separately.
[Locked] Closest Binary Search Tree Value & Closest Binary Search Tree Value II的更多相关文章
- Unable to start activity ComponentInfo{com.example.administrator.myapplication/com.example.administrator.myapplication.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XM
本来就是把fragment写死在activity的xml模板里面,结果报了这个错误, Unable to start activity ComponentInfo{com.example.admini ...
- 错误:android.view.InflateException: Binary XML file line #167: Binary XML file line #167: Error inflating class <unknown>
1:错误日志 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.8.activity.RecordActiv ...
- LeetCode208 Implement Trie (Prefix Tree). LeetCode211 Add and Search Word - Data structure design
字典树(Trie树相关) 208. Implement Trie (Prefix Tree) Implement a trie with insert, search, and startsWith ...
- Mecanim高级主题:Mecanim Blend Tree应用、Blend Tree 选项、复合Blend Tree
一.Blend Tree介绍及应用 一个游戏动画的基本任务就是将两个或多个相似的动作混合.也许最广为人知的例子就是依照任务行动的速度将行走和跑动动画混合起来了.另一个例子就是角色在跑动中向左或向右转身 ...
- eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面
eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面,如果不想覆盖的话,可以这么设置: Window->Preferences->General->Se ...
- [device tree] How to decompile a compiled .dtb (device tree blog) into .dts (device tree source).
$ ./out/target/product/project_name/obj/KERNEL_OBJ/scripts/dtc/dtc -I dtb -O dts -o decompiled.dts ~ ...
- Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介)
原文:Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介) 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...
- Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search)
原文:Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出 ...
- [leetcode tree]98. Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
随机推荐
- Nuget 相关
1:服务器搭建 1.1:创建空的Web Application 1.2:引用NuGet.Server 包:NuGet.Server 安装完成后的界面 web.config 已经被重写了,里面存在一些配 ...
- 1 加到 100 的 时间复杂度 C#.
//1 加到 100 的 时间复杂度: ; ; ; i <= n; i++){ sum += i; } T() = ; //Initialize 'n'. T() = ; //Initializ ...
- UITableViewCell 添加长按手势
UILongPressGestureRecognizer *lpgr = [[UILongPressGestureRecognizer alloc] initWithTarget:self actio ...
- JavaScript Window Screen
window.screen 对象包含有关用户屏幕的信息. Window Screen window.screen对象在编写时可以不使用 window 这个前缀. 一些属性: screen.availW ...
- VB热点答疑(2016.5.11更新Q4、Q5)
收录助教君在VB习题课上最常被问到的问题,每周更新,希望对大家有所帮助. Q1.如何让新的文本内容接在原来的内容后面/下一行显示? A1.例如,Label1.text原本的内容是"VB程序设 ...
- Linux命令:head命令详解
概述:head命令用于显示文件文字区块 1.格式 head [参数][文件] 2.参数 -q 隐藏文件名 -v 显示文件名 -c<字节> 显示字节数 -n<行数> 显示的行数 ...
- SGU 171.Sarov zones
简单的贪心.优先weight最大的,优先匹配Q值大的地区 code #include <iostream> #include <algorithm> #include < ...
- wordpress通过代码禁用IE8, IE9,IE10等IE浏览器兼容视图模式(Compatibility View)
目前wordpress主流主题大多都放弃了对IE6的支持!甚至IE6,IE7,IE8等的兼容模式也不支持!目前特别是国内的双核浏览器大多数使用使用IE内核都是使用的兼容模式!那将是非常糟糕!如何让IE ...
- LGDT/LIDT-加载全局/中断描述符表寄存器
将源操作数中的值加载到全局描述符表寄存器 (GDTR) 或中断描述符表寄存器 (IDTR).源操作数指定 6 字节内存位置,它包含全局描述符表 (GDT) 或中断描述符表 (IDT) 的基址(线性地址 ...
- Html5-Canvas实现简易的抽奖转盘
###Html5实现抽奖转盘效果 1.实现的基本效果 2.主要的内容 html5中canvas标签的使用 jQueryRotate.js旋转插件 3.主要html代码 <body> < ...