Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.

Example:

Input:

   1
\
3
/
2 Output:
1 Explanation:
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).

Note: There are at least two nodes in this BST.

# 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 getMinimumDifference(self, root):
"""
:type root: TreeNode
:rtype: int
"""
diff=10000
stack=[]
node=root
lastvisited=None
while node is not None or stack:
while node:
stack.append(node)
node=node.left
node=stack.pop()
if node is not None and lastvisited is not None:
diff=min(diff,abs(node.val-lastvisited.val))
lastvisited=node
node=node.right return diff

  

[LeetCode&Python] Problem 530. Minimum Absolute Difference in BST的更多相关文章

  1. 【leetcode_easy】530. Minimum Absolute Difference in BST

    problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...

  2. 51. leetcode 530. Minimum Absolute Difference in BST

    530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...

  3. 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  4. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  5. 530. Minimum Absolute Difference in BST

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  6. 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值

    [抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...

  7. leetcode 783. Minimum Distance Between BST Nodes 以及同样的题目 530. Minimum Absolute Difference in BST

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

  8. 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入:   1    \     3    /   2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

  9. 【easy】530. Minimum Absolute Difference in BST

    找BST树中节点之间的最小差值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

随机推荐

  1. 【转】JavaScript数组方法大全

    数组在笔试题中出现的概率最高的类型之一,JavaScript中的数组与其他语言中的数组有些区别,为了方便以后查看数组的方法,现将对数组的操作方法进行汇总整理. 数组创建 JavaScript中创建数组 ...

  2. tls 流量画像——直接使用图像处理的思路探索,待进一步观察

    代码,示意了一个tls的数据内容: import numpy as np import matplotlib.pyplot as pyplot # !!! If on the server, use ...

  3. python实现简单的聊天小程序

    概要 这是一个使用python实现一个简单的聊天室的功能,里面包含群聊,私聊两种聊天方式.实现的方式是使用套接字编程的一个使用TCP协议 c/s结构的聊天室 实现思路 x01 服务端的建立 首先,在服 ...

  4. js正则匹配以某字符串开始字符串

    let decode_sql ="select * from table where create_user='user' order by id desc";   decode_ ...

  5. 把旧系统迁移到.Net Core 2.0 日记(2) - 依赖注入/日志NLog

    Net Core 大量使用依赖注入(Dependency Inject), 打个比方,我们常用的日志组件有Log4Net,NLog等等. 如果我们要随时替换日志组件,那么代码中就不能直接引用某个组件的 ...

  6. python 转换代码格式

    import os dirname="C:\\Users\\haier\\Desktop\\new" def walk(path): for item in os.listdir( ...

  7. Win10系列:UWP界面布局进阶7

    Canvas Canvas元素用于定义一个区域,可以向这个区域中添加不同的XAML界面元素.Canvas会对其内部的元素采用绝对布局方式进行布局,下面通过三个示例来介绍Canvas的使用方法. (1) ...

  8. 实现django admin后台到xadmin后台的转变

    虽然不做前端,还是喜欢好看的东西~.~ 之前同事估计也是功能实现没空管这个后台,前段时间闲的,稍微改了下外貌,前后对比下: Python3.5+Django1.9.7+Xadmin0.6.1 步骤如下 ...

  9. Date和Timestamp区别

    主要是精度问题,date没有ms,而timestamp是有ms的,所以date的精度要低于timestamp. 而且二者可以互相转换. 除此之外,没有什么不同,

  10. jquery 操作table样式拖动参考

    参考: http://blog.csdn.net/kdiller/article/details/6059727 http://www.jb51.net/article/59795.htm