[LeetCode&Python] Problem 783. Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.
Example :
Input: root = [4,2,6,1,3,null,null]
Output: 1
Explanation:
Note that root is a TreeNode object, not an array. The given tree [4,2,6,1,3,null,null] is represented by the following diagram: 4
/ \
2 6
/ \
1 3 while the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.
Note:
- The size of the BST will be between 2 and 100.
- The BST is always valid, each node's value is an integer, and each node's value is different.
# 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 minDiffInBST(self, root):
"""
:type root: TreeNode
:rtype: int
"""
diff=100000
last_visited=None
stack=[]
node=root
while node or stack:
if node:
stack.append(node)
node=node.left
else:
node=stack.pop()
if last_visited and abs(last_visited.val-node.val)<diff:
diff=abs(last_visited.val-node.val)
last_visited=node
node=node.right
return diff
[LeetCode&Python] Problem 783. Minimum Distance Between BST Nodes的更多相关文章
- 【Leetcode_easy】783. Minimum Distance Between BST Nodes
		problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ... 
- 【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 日期 题目地址:https://leetc ... 
- leetcode leetcode 783. Minimum Distance Between BST Nodes
		Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ... 
- 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 ... 
- 783. Minimum Distance Between BST Nodes
		Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ... 
- LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)
		783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ... 
- [LeetCode&Python] Problem 530. Minimum Absolute Difference in BST
		Given a binary search tree with non-negative values, find the minimum absolute difference between va ... 
- LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)
		这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ... 
- [LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离
		Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ... 
随机推荐
- js获取当前iframe的ID
			self.frameElement.getAttribute('id');其他属性也可以通过这种方式获取. 也可以通过 window.frameElement.id 获取当前iframe的ID 
- Struts 2 初步入门(一)
			搭建Struts 2环境步骤 下载jar包----->创建web项目---->创建并完善相关配置文件---->创建action并测试启动 下载jar包访问网站:http://stru ... 
- killl prefix out macro mis mal micro -m
			1● macro 宏大,规模大 2● mis 错误,坏 3● mal 坏,错误 4● micro 小,微小 
- learning ddr input clock frequency change condition
- bzoj1968
			题解: 显然每一个数对答案的贡献为n/i 代码: #include<bits/stdc++.h> using namespace std; int n; int main() { scan ... 
- OO作业总结(三)
			类规格设计 由于没能找到关于类规格设计的发展历史,所以结合程序设计思想的发展来谈谈规格化设计. 最早的程序设计都是采用机器语言来编写的,直接使用二进制码来表示机器能够识别和执行的指令和数 据.简单来说 ... 
- Java连接SqlServer 2008数据库
			将sqljdbc4.jar包添加到工程 连接SqlServer 2008数据库 import java.sql.Connection; import java.sql.DriverManager; i ... 
- Android : apk签名的多种方法以及key的配置
			方法一:使用Android SDK中的签名工具给apk签名: (1)Android源码的 build/target/product/security/ 目录下有 media.pk8.media.x50 ... 
- 2.5 C++类class和结构体struct区别
			参考:http://www.weixueyuan.net/view/6337.html 总结: 在C++中,struct类似于class,在其中既可以定义数据成员,又可以定义成员函数. 在C++中,s ... 
- springboot学习章节-spring常用配置
			1.Scope package com.zhen.highlights_spring4.ch2.scope; import org.springframework.context.annotation ... 
