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:

  1. The size of the BST will be between 2 and 100.
  2. The BST is always valid, each node's value is an integer, and each node's value is different.

这道题跟之前那道Minimum Absolute Difference in BST没有任何区别,解法完全可以共用,讲解也可以参见之前的帖子,这里就简略的说一下。第一种方法很直接,通过中序遍历按顺序从小到大将所有的结点值都存入到一个数组中,然后就遍历这个数组,找相邻的两个的差值最小的返回即可,参见代码如下:

解法一:

class Solution {
public:
int minDiffInBST(TreeNode* root) {
int res = INT_MAX;
vector<int> v;
helper(root, v);
for (int i = ; i < v.size(); ++i) {
res = min(res, v[i] - v[i - ]);
}
return res;
}
void helper(TreeNode* node, vector<int>& vals) {
if (!node) return;
helper(node->left, vals);
vals.push_back(node->val);
helper(node->right, vals);
}
};

我们可以优化上面解法的空间复杂度,并不记录所有的结点值,而是只记录之前的结点值,然后做差值更新结果res即可。

解法二:

class Solution {
public:
int minDiffInBST(TreeNode* root) {
int res = INT_MAX, pre = -;
helper(root, pre, res);
return res;
}
void helper(TreeNode* node, int& pre, int& res) {
if (!node) return;
helper(node->left, pre, res);
if (pre != -) res = min(res, node->val - pre);
pre = node->val;
helper(node->right, pre, res);
}
};

其实我们也不必非要用中序遍历不可,用先序遍历同样可以利用到BST的性质,我们带两个变量low和high来分别表示上下界,初始化为int的极值,然后我们在递归函数中,分别用上下界和当前节点值的绝对差来更新结果res,参见代码如下:

解法三:

class Solution {
public:
int minDiffInBST(TreeNode* root) {
int res = INT_MAX;
helper(root, INT_MIN, INT_MAX, res);
return res;
}
void helper(TreeNode* node, int low, int high, int& res) {
if (!node) return;
if (low != INT_MIN) res = min(res, node->val - low);
if (high != INT_MAX) res = min(res, high - node->val);
helper(node->left, low, node->val, res);
helper(node->right, node->val, high, res);
}
};

下面这种方法是解法一的迭代的写法,思路跟之前的解法没有什么区别,参见代码如下:

解法四:

class Solution {
public:
int minDiffInBST(TreeNode* root) {
int res = INT_MAX, pre = -;
stack<TreeNode*> st;
TreeNode* p = root;
while (!st.empty() || p) {
if (p) {
st.push(p);
p = p->left;
} else {
p = st.top(); st.pop();
if (pre != -) res = min(res, p->val - pre);
pre = p->val;
p = p->right;
}
}
return res;
}
};

类似题目:

Minimum Absolute Difference in BST

Binary Tree Inorder Traversal

参考资料:

https://leetcode.com/problems/minimum-distance-between-bst-nodes/solution/

https://leetcode.com/problems/minimum-distance-between-bst-nodes/discuss/114834/Inorder-Traversal-O(N)-time-Recursion-C++JavaPython

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离的更多相关文章

  1. Leetcode783.Minimum Distance Between BST Nodes二叉搜索树结点最小距离

    给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意,root是树结点对象(Tr ...

  2. [LeetCode] Inorder Successor in BST II 二叉搜索树中的中序后继节点之二

    Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...

  3. [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  4. [LeetCode] 450. Delete Node in a BST 删除二叉搜索树中的节点

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

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

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

  6. 450 Delete Node in a BST 删除二叉搜索树中的结点

    详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...

  7. LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)

    783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...

  8. LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)

    230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...

  9. 【Leetcode_easy】783. Minimum Distance Between BST Nodes

    problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...

随机推荐

  1. 阿里云ECS服务器Ubuntu安装MySQL并远程访问

    root账户登录服务器Ubuntu16.04 apt-get update apt-get install mysql-server mysql-client; 安装时会让你设置root密码,输入2次 ...

  2. [数学笔记Mathematical Notes]目录

    2.也许是一个问题,暂时没给出解答. 2015年7月5日 1. 这个一个笔记类型的数学杂志, 打算用来记录自己学数学时做的笔记,一般几页纸一期. 觉得有意思就摘抄下来,或者自己的感想. 可能有些不是原 ...

  3. [再寄小读者之数学篇](2014-06-22 发散级数 [中国科学技术大学2012年高等数学B考研试题])

    设 $a_n>0$, $S_n=a_1+a_2+\cdots+a_n$, 级数 $\dps{\vsm{n}a_n}$ 发散, 证明: $\dps{\vsm{n}\cfrac{a_n}{S_n}} ...

  4. 408 JavaScript 变量、数据类型、正则

    JavaScript 特点 是一门解释性脚本语言 .基于对象脚本编程.简单性(弱类型).安全性.动态性.跨平台 作用: 初学js 引入方式 与html有相同之处 也是3种1 用JavaScript前缀 ...

  5. lucene学习的小结

    pom.xml设置 <dependency> <groupId>junit</groupId> <artifactId>junit</artifa ...

  6. OpenCV掩模mask的原理和作用

    一.什么是掩模mask OpenCV中很多函数都带有一个mask参数,mask被称为掩模.图像掩模一般用来对处理的图像(全部或者局部)进行遮挡,来控制图像处理的区域或处理过程. 二.掩模原理 掩模一般 ...

  7. 【转载】MySQL5.7 添加用户、删除用户与授权

    mysql -uroot -proot MySQL5.7 mysql.user表没有password字段改 authentication_string: 一. 创建用户: 命令:CREATE USER ...

  8. sort和priority_queue的比较函数总结

    对于priority_queue来说,,比较函数为(如果不是结构体,直接int,优先队列默认的是值越大优先级越大): struct st { string str; int pr, value,mar ...

  9. js分析 汽_车_之_家 js生成css伪元素 hs_kw44_configUS::before

    0.参考 https://developer.mozilla.org/zh-CN/docs/Web/CSS/Pseudo-elements https://developer.mozilla.org/ ...

  10. Swift - use Array

    //数组声明 var arr0 = Array<Int>() var arr1 = Array<String>(count: 3, repeatedValue: "& ...