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:

    1. Consider implement these two helper functions:
      1. getPredecessor(N), which returns the next smaller node to N.
      2. getSuccessor(N), which returns the next larger node to N.
    2. Try to assume that each node has a parent pointer, it makes the problem much easier.
    3. Without parent pointer we just need to keep track of the path from the root to the current node using a stack.
    4. 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的更多相关文章

  1. 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 ...

  2. 错误: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 ...

  3. 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  ...

  4. Mecanim高级主题:Mecanim Blend Tree应用、Blend Tree 选项、复合Blend Tree

    一.Blend Tree介绍及应用 一个游戏动画的基本任务就是将两个或多个相似的动作混合.也许最广为人知的例子就是依照任务行动的速度将行走和跑动动画混合起来了.另一个例子就是角色在跑动中向左或向右转身 ...

  5. eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面

    eclipse在search的时候,通过search打开的页面会覆盖之前打开的页面,如果不想覆盖的话,可以这么设置: Window->Preferences->General->Se ...

  6. [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 ~ ...

  7. Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介)

    原文:Elasticsearch7.X 入门学习第四课笔记---- Search API之(Request Body Search 和DSL简介) 版权声明:本文为博主原创文章,遵循CC 4.0 BY ...

  8. Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search)

    原文:Elasticsearch7.X 入门学习第三课笔记----search api学习(URI Search) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出 ...

  9. [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 ...

随机推荐

  1. postgresql sql修改表,表字段

    1.更改表名 alter table 表名 rename to 新表名 2.更改字段名 alter table 表名 rename 字段名 to 新字段名 3.增加列 ALTER TABLE ud_w ...

  2. vs编译和运行的区

    编译: 是把代码转变成一系列指令(把源代码翻译为计算机能够识别的语言),产生目标代码,并不限于EXE(EXE只是WINDOWS的东西),这样才能装入内存; 运行: 是运行目标代码(运行EXE),就是执 ...

  3. power desinger 学习笔记<五>

    怎样才能在修改表的字段Name的时候,Code不自动跟着变 tools-> General   Options-> Dialog:Operation   Modes: 去掉 NameToC ...

  4. JavaScript Boolean(布尔) 对象

    创建 Boolean 对象 Boolean 对象代表两个值:"true" 或者 "false" 下面的代码定义了一个名为 myBoolean 的布尔对象: va ...

  5. JavaScript HTML DOM 元素(节点)

    JavaScript HTML DOM 元素(节点) 创建新的 HTML 元素 创建新的 HTML 元素 如需向 HTML DOM 添加新元素,您必须首先创建该元素(元素节点),然后向一个已存在的元素 ...

  6. OpenCV中Mat的详解

    每次碰到Mat都得反复查具体的用法,网上的基础讲解不多,难得看到一篇,赶快转来收藏~ 原文地址:http://www.opencvchina.com/thread-1039-1-1.html 目标 我 ...

  7. 异步IO简介

    最近想学习一下libevent,就先翻译一下libevent的官方文档吧. 英文原文链接:http://www.wangafu.net/~nickm/libevent-book/01_intro.ht ...

  8. aix7安装was7、打补丁、更改访问端口、手动启动was、配置was7、部署项目

    1:准备工作  首先了解下我们下面即将用到的aix命令,以及安装包.补丁安装工具.补丁 was7的安装包以及补丁工具都是压缩包形式并且以.tar.gz结尾的 安装包在800MB左右,通常为****_w ...

  9. [jQuery编程挑战]005 使用最短的代码生成元素的闪烁效果

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  10. dedecms由子目录访问修改为根目录访问

    现在我需要将原来位于xampp/htdocs/zm下的网站修改到D:/lyh/webhome目录下访问,原来的访问路径为http://localhost/zm,现在为http://www.yihui. ...