[LeetCode&Python] Problem 530. Minimum Absolute Difference in BST
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的更多相关文章
- 【leetcode_easy】530. Minimum Absolute Difference in BST
problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...
- 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 ...
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
[抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...
- 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 ...
- 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入: 1 \ 3 / 2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...
- 【easy】530. Minimum Absolute Difference in BST
找BST树中节点之间的最小差值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
随机推荐
- Linux+Apache+MySQL+PHP配置教程
有时我们只想搭建LAMP环境做个测试,并不在意目录的和配置是否规范,本教程正是为此想法而写能简单的就不复杂实现最快地搭建LAMP:操作系统为CentOS6.5. 1.安装Apache yum inst ...
- Vmware tools install
Vmware tools 1◆ 下载 2◆ diagram Ifcfg-eth0 =====>关闭防火墙 systemctl stop firewalld.service ===== ...
- textext for Inkscape
http://askubuntu.com/questions/417212/inkscape-with-textext http://www.timteatro.net/2010/08/05/text ...
- 蓝桥杯—BASIC-25 回形取数
题目:回形取数就是沿矩阵的边取数,若当前方向上无数可取或已经取过,则左转90度.一开始位于矩阵左上角,方向向下.输入格式 输入第一行是两个不超过200的正整数m, n,表示矩阵的行和列.接下来m行每行 ...
- centos 安装 TortoiseSVN svn 客户端
1 安装 svn客户端 yum install -y subversion 2 常用命令操作 检出命令 svn checkout http://svn.com/path
- JS-封装类或对象的最佳方案
JS封装类或对象的最佳方案 面向对象强大的优点之一是能够创建自己专用的类或者对象,封装一组属性和行为.抛开性能来说,JS要比面向对象语言如JAVA要灵活好用的多,组装数据结构很灵活方便.那么我们如何来 ...
- bzoj4237
题解: cdq分治 二位变成一维 二分一下 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; ; ...
- 写一个xml文件到磁盘的方法
/** * 往磁盘上写一个xml文件 * * <?xml version="1.0" encoding="UTF-8" standalone=" ...
- linux:centOs7没有eth0网卡
1.修改ifcfg-ens33为ifcfg-eth0 cd /etc/sysconfig/network-scripts/ su root #进入root模式,需要输入 ...
- Python实现登陆的功能
import datetimetoday=datetime.datetime.today()# 获取当前时间for i in range(3): username=input("请输入用户名 ...