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的更多相关文章

  1. LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  2. 【leetcode_easy】530. Minimum Absolute Difference in BST

    problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...

  3. 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  4. [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 ...

  5. 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值

    [抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...

  6. 530. Minimum Absolute Difference in BST

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  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. 【easy】530. Minimum Absolute Difference in BST

    找BST树中节点之间的最小差值. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode ...

  9. 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差

    给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入:   1    \     3    /   2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...

随机推荐

  1. 从一道例题谈Arrays.toString()与其他String的转换方法

    阅读该篇文章前,请大家事先阅读一下:   java.toString(),(String),String.valueOf的区别 有了上述基础后,我接下来谈谈从一道题目中获得的些许收获. 今天在做题是发 ...

  2. webpack前端工程化构建工具的使用

    一.模块打包机 1.创建文件 在目标文件下建立一个src文件夹作为js代码区:作为例子,我创建了两个js文件,并利用commonJS规范require引入到index.js中: moduleA.js: ...

  3. Swift数组字面量

    可以用一个数组字面量来初始化一个数组,简单地把一个或多个值放在一起就可以了.数组字面量的写法是一行用逗号隔开的值,并在行的两端用一对方括号包起来: [value , value , value ] 下 ...

  4. apache 基本vhost配置

    经常使用Apache虚拟主机进行开发和测试,但每次需要配置虚拟主机时都习惯性的ctrl+c和ctrl+v,这次由于重装系统,需要配置一个新的PHP开发环境虚拟主机,于是总结一下Apaceh配置http ...

  5. Spring事务管理注意小事项

    在service类前加上@Transactional,声明这个service所有方法需要事务管理.每一个业务方法开始时都会打开一个事务. Spring默认情况下会对运行期例外(RunTimeExcep ...

  6. 【LeetCode】205. Isomorphic Strings

    题目: Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the c ...

  7. 【Android Developers Training】 98. 获取联系人列表

    注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...

  8. tab切换实现方式1

    tab切换实现方式1: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  9. Vim正则通配符使用心得

    目的 实现替换 c f[i][j][k] -> f[k][i][j] f[i + 1][j][k] -> f[k][i + 1][j] f[i + 1][j + NY][k] -> ...

  10. HDFS Java API的使用举例

    HDFS是Hadoop应用程序使用的主要分布式存储.HDFS集群主要由管理文件系统元数据的NameNode和存储实际数据的DataNodes组成,HDFS架构图描述了NameNode,DataNode ...