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 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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- javascript中在链表中向前(向后)移动n个节点
1.概念 在链表上移动n个节点,我第一眼看到这个需求的时候首先想到的是当前节点.使用这个当前节点作为参考来移动,没有这个当前节点的话是没有办法在链表上前进和后退的.初始化定义链表的时候定义一个当前节 ...
- 轉發XML
Delphi 中的 XMLDocument 类详解(3) - 读取 xml 文件 先虚拟一个测试文件: test.xml; 放在 c:\temp\ 下备用. <?xml version=&quo ...
- [py]特殊函数+文件保护
1函数的好处 2函数的全局变量和局部变量 3,包和文件夹的区别 4,__name__ __file__ __doc__ #判断是否为主程序 if __name__=='__main__': pass ...
- android:ToolBar详解(手把手教程)(转)
来源 http://blog.mosil.biz/2014/10/android-toolbar/ 编辑推荐:稀土掘金,这是一个针对技术开发者的一个应用,你可以在掘金上获取最新最优质的技术干货,不仅仅 ...
- QT 网络编程三(TCP版)
QT客户端 //widget.h #ifndef WIDGET_H #define WIDGET_H #include <QWidget> #include <QTcpSocket& ...
- c语言 指针与数组
关键概念: 1.多个不同类型的指针可以对应同一个地址: 2.(&p)则是这样一种运算,返回一个指针,该指针的值是当时声明p 时开辟的地址,指针的类型是p的类型对应的指针类型: 3.(*p)操作 ...
- promise的学习
为了解决回调地狱的问题,所以出现了promise的设计思想. promise的三种状态: pending 等待状态 resolved 完成状态 rejected 拒绝状态 promise的三种状态,只 ...
- 数据契约(DataContract)及序列化指定输出字段
服务契约定义了远程访问对象和可供调用的方法,数据契约则是服务端和客户端之间要传送的自定义数据类型. 一旦声明一个类型为DataContract,那么该类型就可以被序列化在服务端和客户端之间传送,如下所 ...
- 网络功能虚拟化(NFV)
你造什么是网络功能虚拟化(NFV)吗? NFV将网络功能整合到行业标准的服务器.交换机和存储硬件上,提供了优化的虚拟化数据平面,NFV通过服务器上运行的软件让管理员取代传统物理网络设备,并降低成本.能 ...
- arguments.callee 调用自身 caller,callee,apply and call
一.Arguments该对象代表正在执行的函数和调用他的函数的参数.[function.]arguments[n]参数function :选项.当前正在执行的 Function 对象的名字.n :选项 ...