Closest Binary Search Tree Value -- LeetCode
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.
思路:递归求解。因为是二叉搜索树,我们不需要遍历所有的节点,通过prune来提高速度。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int closestValue(TreeNode* root, double target) {
int res = root->val;
if (root->left != NULL && (double)res > target) {
int leftRes = closestValue(root->left, target);
res = abs(res - target) < abs(leftRes - target) ? res : leftRes;
}
if (root->right != NULL && (double)res < target) {
int rightRes = closestValue(root->right, target);
res = abs(res - target) < abs(rightRes - target) ? res : rightRes;
}
return res;
}
};
Closest Binary Search Tree Value -- LeetCode的更多相关文章
- [LeetCode] 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 t ...
- [LeetCode] 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 t ...
- LeetCode Closest Binary Search Tree Value II
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value-ii/ 题目: Given a non-empty bin ...
- LeetCode Closest Binary Search Tree Value
原题链接在这里:https://leetcode.com/problems/closest-binary-search-tree-value/ Given a non-empty binary sea ...
- [LeetCode] 272. 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 t ...
- [LeetCode] 272. Closest Binary Search Tree Value II 最近的二叉搜索树的值 II
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- Closest Binary Search Tree Value I & II
Closest Binary Search Tree Value Given a non-empty binary search tree and a target value, find the v ...
- 272. 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 close ...
- [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 ...
随机推荐
- QA 、 QC & QM软件测试入门专业名词解释 -- 灌水走起
灌水正式开始: 说明:我的农田,我灌水 一.QA . QC & QM: 1.QM QM 是quanlity management,中文名称是品质管理 2.QA QA是英文quality ass ...
- ASP NET Core 部署 IIS 和发布
1. 微软官网原文链接: https://docs.microsoft.com/zh-cn/aspnet/core/host-and-deploy/iis/index?view=aspnetcore- ...
- Android 启动项 Activity
使用SDK创建一个App的时候,基本都会看到这段代码: <activity android:name=".ExampleActivity" android:icon=&quo ...
- css深入理解vertical-align
第一讲:vertical-align家族基本认识 了解vertical-align支持的属性值以及组成 属性: 1.inherit 2.线类 baseline,top,middle,bottom 3. ...
- php中普通方法和静态方法的区别以及抽象类和接口
实例化类产生对象.class fenbi{ //普通成员,属于对象 public $length = "10cm"; //静态成员,静态变量,属于类. public static ...
- stack,heap的区别
一个由C/C++编译的程序占用的内存分为以下几个部分 1.栈区(stack)— 由编译器自动分配释放 ,存放函数的参数值,局部变量的值等.其 操作方式类似于数据结构中的栈. ...
- 【Luogu】P3288方伯伯运椰子(消圈定理)
题目链接 分数规划题,详见luogu题解 #include<cstdio> #include<cstring> #include<cctype> #include& ...
- 用canvas实现鼠标拖动绘制矩形框
需要用到jCanvas插件和jQuery. jCanvas下载:https://raw.githubusercontent.com/caleb531/jcanvas/master/jcanvas.mi ...
- task [最大权闭合子图]
题面 思路 其实仔细读透就发现,是一个最大权闭合子图的模型 套进网络流里面就挺好做的了 可以选择重载这道题里面的一些运算(加减,取最小值),这样比较方便 Code #include<iostre ...
- hdu 6010 路径交
hdu 6010 路径交(lca + 线段树) 题意: 给出一棵大小为\(n\)的树和\(m\)条路径,求第\(L\)条路径到第\(R\)条路径的交的路径的长度 思路: 本题的关键就是求路径交 假设存 ...