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.

沿着root向下找即可,每次判段一下是在root左边还是右边。注意没有child的情况。

 # Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def closestValue(self, root, target):
"""
:type root: TreeNode
:type target: float
:rtype: int
"""
#if target == root.val:
# return root.val if target < root.val:
if not root.left:
return root.val
else:
c1 = self.closestValue(root.left, target)
if abs(c1 - target) < abs(root.val - target):
return c1
else:
return root.val
else:
if not root.right:
return root.val
else:
c2 = self.closestValue(root.right, target)
if abs(c2 - target) < abs(root.val - target):
return c2
else:
return root.val

Leetcode 270. Closest Binary Search Tree Value的更多相关文章

  1. [LeetCode] 270. 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 ...

  2. [leetcode]270. Closest Binary Search Tree Value二叉搜索树中找target的最接近值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

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

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

  5. 270. Closest Binary Search Tree Value 二叉搜索树中,距离目标值最近的节点

    [抄题]: Given a non-empty binary search tree and a target value, find the value in the BST that is clo ...

  6. 270. Closest Binary Search Tree Value

    题目: Given a non-empty binary search tree and a target value, find the value in the BST that is close ...

  7. [LeetCode#272] Closest Binary Search Tree Value II

    Problem: Given a non-empty binary search tree and a target value, find k values in the BST that are ...

  8. [leetcode]272. Closest Binary Search Tree Value II二叉搜索树中最近的值2

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  9. [LC] 270. 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 ...

随机推荐

  1. Notes: sensitivity & specificity

    terminology: True positive (TP); False positive (FP): originally negative; True negative (TN); False ...

  2. MathType 公式后的空格问题

    注册表编辑器修改 Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Design Science\DSMT6\WordC ...

  3. Visual Studio 2013编辑HTML文件无设计视图的解决方案

    在Visual Studio 2013中编辑HTML文件,会发现没有设计视图. 解决方法:点击Visual Studio 2013的”工具“菜单,再点击”选项“—>文本编辑器—>文件扩展名 ...

  4. Linux 网络编程详解四(流协议与粘包)

    TCP/IP协议是一种流协议,流协议是字节流,只有开始和结束,包与包之间没有边界,所以容易产生粘包,但是不会丢包. UDP/IP协议是数据报,有边界,不存在粘包,但是可能丢包. 产生粘包问题的原因 . ...

  5. QT 数据库编程三

    //mainwindow.cpp #include "mainwindow.h" #include "logindlg.h" #include "sc ...

  6. Nginx+Varnish 实现动静分离,为服务器分流,降低服务器负载

    相必大家在看加快网站响应速度方面的文章时,都提过这么一条:动静分离.那怎样实现动静分离呢,这里笔者就亲自搭建相关服务实现动静分离. 动静分离是一种架构,就是把静态文件,比如JS.CSS.图片甚至有些静 ...

  7. 拦截PHP各种异常和错误,发生致命错误时进行报警,万事防患于未然

    在日常开发中,大多数人的做法是在开发环境时开启调试模式,在产品环境关闭调试模式.在开发的时候可以查看各种错误.异常,但是在线上就把错误显示的关闭. 上面的情形看似很科学,有人解释为这样很安全,别人看不 ...

  8. SqlServer导入数据到MySql

    1.下载MySql ODBC Driver并进行安装.例如我下载的这个安装包是mysql-connector-odbc-5.1.6-win32.msi. 2.装完后,添加odbc数据源: 3.在sql ...

  9. ModernUI教程:如何从MUI样式中派生自定义样式

    下面的步骤用来说明怎么样去创建一个基于MUI的自定义样式.让我们创建一个字体颜色显示为红色的按钮样式. 可视化显示如下: 因为我们并没有明确生命继承自MUI风格,它还是采用WPF的默认风格.我们需要设 ...

  10. Redis Sentinel:集群Failover解决方案(转载)

    本文转载自:http://shift-alt-ctrl.iteye.com/blog/1884370 文中的配置例子,还有failover过程中触发的订阅事件具有很好的参考价值. Redis sent ...