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. DirectX11 With Windows SDK--14 深度测试

    前言 当使用加法/减法/乘法颜色混合,或者使用透明混合的时候,在经过深度测试时可能会引发一些问题.例如现在我们需要使用加法混合来绘制一系列对象,而这些对象彼此之间不会相互阻挡.若我们仍使用原来的深度测 ...

  2. CemtOS7更改yum网易 阿里云的yum源。

    一,鉴于用国外的Yum源,速度比较慢,所以想到将国外的yum源,改为国内的Yum源,著名的有网易 阿里云源.如何更改呢? 二,更改yum源为网易的. 首先备份/etc/yum.repos.d/Cent ...

  3. liunx系统下调整Swap分区大小

    作者:邓聪聪 添加swap交换空间的步骤如下:第一步:确保系统中有足够的空间来用做swap交换空间,准备在一个独立的文件系统中添加一个swap交换文件,在/tmp中添加1G的swap交换文件第二步:添 ...

  4. C#VS2017添加ReportViewer控件

    安装完vs2017之后我们进行添加Report Viewer控件: 1. 点击Tools -> Extensions and Updates... 2. 在新窗口搜索栏中输入rdlc后搜索,结果 ...

  5. linux网关下drcom web自动登陆脚本

    /etc/init.d/drcomd: #!/bin/sh # # The environment is cleared before executing this script # so the p ...

  6. 【原创】大数据基础之Hadoop(3)yarn数据收集与监控

    yarn常用rest api 1 metrics # curl http://localhost:8088/ws/v1/cluster/metrics The cluster metrics reso ...

  7. $a=[1,2,3,4,5]; $b=[a,b,c,d,e]; 转成[[1,a],[2,b],[3,c],[4,d],[5,3]]

    $a=[1,2,3,4,5]; $b=[a,b,c,d,e]; 结果 [[1,a],[2,b],[3,c],[4,d],[5,3]] return array_map(function($v1,$v2 ...

  8. MySQL批量更新字段url链接中的域名

    1. 首先进行数据库备份 2. SQL语句 UPDATE 表名 SET 字段 = REPLACE(字段, '待更新的内容','替换值'); eg: UPDATE 98k_images SET url ...

  9. Hadoop第一式:配置Linux环境

    所有操作在虚拟机下完成,虚拟机软件选用VMware Workstation Pro 12 (后文简称为VM) 关于Linux安装不再阐述一.网络环境配置 1)Windows界面 首先在VM页面,点击虚 ...

  10. linux下Flask框架搭建简单网页

    开始安装FLASK需要创建一个虚拟环境,虚拟环境可以不干扰正在使用的系统环境,避免影响,并且也不需要完全的root权限,更加安全可靠. 搭建环境 Python3.4 进入到microblog目录下创建 ...