51. leetcode 530. Minimum Absolute Difference in BST
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.
我的思路:d1:如果根节点左子树不为空,则设置为根节点与左子树的差,如果根节点的左子树的右子树不为空,则继续把d1设置为根节点和它的左子树的深度最深的右子节点的差;d2:根节点的左子树中最小的差值;
同样处理d3和d4,如果根节点的右子树不为空,则将d3设置为根节点与右子树的差,如果根节点的右子树的左子节点不为空,则继续设置d3为根节点的值和根节点的右子树的深度最深的左子节点的差,d4设置为右子树中最小的差值。

改进思路:以上思路利用了搜索二叉树的性质,最根本的性质就是:搜索二叉树的中序遍历序列是一个递增序列。可以先得到中序遍历的结果,然后再找最小差值,在一个递增序列中,最小差值一定出现在相邻元素之间。

当然,以上是利用递归方式对树进行遍历的,我们也可以利用非递归。要熟练掌握二叉树的中序遍历方法。详见大黑皮笔记本上的笔记。

51. leetcode 530. Minimum Absolute Difference in BST的更多相关文章
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 【leetcode_easy】530. Minimum Absolute Difference in BST
problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- [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 ...
- 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
[抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...
- 530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 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 ...
- 【easy】530. Minimum Absolute Difference in BST
找BST树中节点之间的最小差值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...
- 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入: 1 \ 3 / 2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...
随机推荐
- module.exports与exports,export和export default
还在为module.exports.exports.export和export default,import和require区别与联系发愁吗,这一篇基本就够了! 一.首先搞清楚一个基本问题: modu ...
- 详解 RAC 中各种IP和监听的意义
一.SCAN 概念 SCAN(Single Client Access Name)是 Oracle从11g R2开始推出的,客户端可以通过 SCAN 特性负载均衡地连接到 RAC数据库 SCAN 最明 ...
- linux 下tomcat的安装
写在前面: 由于项目使用jdk1.6开发,所以对应服务器应安装jdk1.6和tomcat6 --- 1.环境变量的配置: 打开/etc/bashrc配置环境变量 JAVA_HOME=/usr/apps ...
- jquery删除表格行
$(".mingxirmspan").click(function(){ $(this).closest("tr").remove(); })
- php的laravel数据库版本管理器migration
第一步:连接数据库 打开.env文件.配置DB_HOST DB_PORT DB_DATABASE=LARAVEL DB_USERNAME DB_PASSWORD 注意DB_DATABASE这一项需要自 ...
- Pycharm直接连接Github
Pycharm可以说是使用Python语言开发者的必备利器.高校学生有学生邮箱就可以免费使用,着实省了我不少银两.附个license图: Git是一个开源的分布式版本控制系统,用以有效.高速的处理从很 ...
- DOCKER 从入门到放弃(三)
使用docker create [image-name] 创建一个容器 创建一个nginx镜像的容器,由于没有指定各项参数,容器实用默认参数,创建后并不会启动,并将容器的ID输出到终端,如果本地没有镜 ...
- Oracle数据库中的重要对象
数据库中的重要对象:表.视图.序列.函数.存储过程.索引.同义词1.表:用PL/SQL Developer 软件打开 Scott的DEPT表查看SQL,可以看见DEPT表创建的脚本-- Create ...
- 【SEO】搜索引擎优化的陷阱和作弊
一.认识SEO [理解] 站内优化是指更改网站内部结构,让网站利于蜘蛛爬取,比如网站内容: 站外优化是指发反向链接,给蜘蛛一个爬取你网站的通道. 其中,反向链接是指网页A 上有一个链接指向网页B,则网 ...
- VB6之HOOK技术
代码背景,自身程序的窗口上有一个TextBox,Hook住WH_CALLWNDPROC用来截获EN_CHNAGE即文本变更的消息. *这个其实用SetWindowLong和CallWindowProc ...