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. sudo配置教程

    一.相关说明 1.sudo配置文件是/etc/sudoers:另外会自动包含/etc/sudoers.d目录下的文件(/etc/sudoers文件最后有一句“#includedir /etc/sudo ...

  2. pycharm搭建开发配置,远程调试,数据库配置,git配置等

    1 开发环境搭建 1.1 简介 使用虚拟机作为代码运行环境,本地使用pycharm进行代码编辑,使用远程调试功能进行debug. 1.1 安装centos虚拟机环境: 1.操作系统: 2.网络配置: ...

  3. Centos 7.4 源码 Nginx 安装

    一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel 二.首先要安装 PCRE ...

  4. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  5. CSS3 的box-shadow进阶之 - 动画篇 - 制作辐射动画

    本篇文章是上一篇讲box-shadow基础知识的延伸,建议先花几分钟阅读那篇文章,点击阅读,再来看这篇. 除了box-shadow属性知识外,制作动画,还需要对CSS3的animation, @key ...

  6. QuickStart系列:docker部署之Mysql

    这里配置只做开发用,生产环境请根据需要修改或自行搜索其他说明 使用docker安装mysql,目前版本5.7.4(当前时间 2018.1.11) 环境 vm: Centos7 镜像来源 https:/ ...

  7. linux操作系统及命令Part 2

    cat 命令 cat .txt .txt .txt > Ta.txt 将左边三个文件纵向合并为Ta文件 cat .txt>> Ta.txt 将左边文件的内容添加到Ta文件中 tar ...

  8. Resharper插件的使用

    一.Resharper设置 1.1 智能提示 安装完毕后,IDE 的智能提示(Intellisense)便会默认使用 Resharper 的提示,不知道为什么,我一直不太喜欢它的提示.改过来,是在Op ...

  9. 遍历存储所有物体添加到列表中(使用GameObject.activeSelf进行判断)

    //存储菜单列表 List<GameObject> subMenu = new List<GameObject>(); //存储所有子菜单 public void StoreS ...

  10. GDI中StretchBlt或Blt缩放图片失真问题

    在用Scrollview控件的缩放图片时,严重失真: 解决方法:dc.SetStretchBltMode(COLORONCOLOR). 参考文章:https://blog.csdn.net/m3728 ...