【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/minimum-distance-between-bst-nodes/description/
题目描述
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.
题目大意
求BST的两个节点之间的最小差值。
解题方法
中序遍历
看见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 minDiffInBST(self, root):
"""
:type root: TreeNode
:rtype: int
"""
vals = []
def inOrder(root):
if not root:
return
inOrder(root.left)
vals.append(root.val)
inOrder(root.right)
inOrder(root)
return min([vals[i + 1] - vals[i] for i in xrange(len(vals) - 1)])
二刷的时候注意到和530. Minimum Absolute Difference in BST是完全一样的题,果然同样的代码就直接通过了。。不懂这个题的意义是什么。。
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def minDiffInBST(self, root):
"""
:type root: TreeNode
:rtype: int
"""
self.res = float("inf")
self.prev = None
self.inOrder(root)
return self.res
def inOrder(self, root):
if not root: return
self.inOrder(root.left)
if self.prev:
self.res = min(self.res, root.val - self.prev.val)
self.prev = root
self.inOrder(root.right)
日期
2018 年 2 月 28 日
2018 年 11 月 14 日 —— 很严重的雾霾
【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)的更多相关文章
- 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 ...
- 【Leetcode_easy】783. Minimum Distance Between BST Nodes
problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...
- [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 ...
- 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】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 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 ...
随机推荐
- Linux生产应用常见习题汇总
1.如果想修改开机内核参数,应该修改哪个文件? C A./dev/sda1 (scsi sata sas,是第1块盘的第1个分区) B./etc/fstab (开机磁盘自动挂载配置文件) C./etc ...
- JVM2 类加载子系统
目录 类加载子系统 类加载器子系统 类加载器ClassLoader角色 类加载的过程 案例 加载Loading 连接Linking 初始化Intialization clinit() 类的加载器 虚拟 ...
- Mysql索引数据结构详解(1)
慢查询解决:使用索引 索引是帮助Mysql高效获取数据的排好序的数据结构 常见的存储数据结构: 二叉树 二叉树不适合单边增长的数据 红黑树(又称二叉平衡树) 红黑树会自动平衡父节点两边的 ...
- day28 进程(Process)
day28 进程(Process) 1.进程的概念 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础. # 进程是系统进行 ...
- IDEA2021.2安装与配置
https://blog.csdn.net/qq_37242720/article/details/119349394
- electron搭建开发环境
环境:windons10, nodev14.17.1, vscode md a_star cd a_star npm i -g yarn yarn config set ELECTRON_MIRROR ...
- size_type 和 size_t 的区别
标准库string里面有个函数size,用来返回字符串中的字符个数,具体用法如下:string st("The expense of spirit\n");cout << ...
- 【Spring Framework】Spring入门教程(四)注册Bean到IOC容器
注册Bean到IOC容器大致分为4种: ①.包扫描+组件注解(@Controller.@Service.@Repository.@Component) 针对类是我们自己编写的情况 ②.@Bean注解 ...
- SOUI3界面编辑器使用说明
SOUI一直没有官方的界面编辑器,关键是我自己一直坚持手写界面更好控制. 大概是2年前,网友"指尖"开发了一个SOUI2的编辑器,功能非常多,特点是可以拖动控件来实现可视化布局. ...
- rpm-build方式制作rpm包
目录 一.简介 二.具体操作 一.简介 可以将编译完成的服务打成rpm包放到私有仓库了,用于自定义的各种软件进行安装部署配置. 二.具体操作 1.安装软件,这个命令将构建rpm包 yum -y ins ...