[LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离
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.
这道题跟之前那道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
参考资料:
https://leetcode.com/problems/minimum-distance-between-bst-nodes/solution/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离的更多相关文章
- Leetcode783.Minimum Distance Between BST Nodes二叉搜索树结点最小距离
给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意,root是树结点对象(Tr ...
- [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 ...
- [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 ...
- [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 ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 450 Delete Node in a BST 删除二叉搜索树中的结点
详见:https://leetcode.com/problems/delete-node-in-a-bst/description/ C++: /** * Definition for a binar ...
- LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)
783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...
- LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ...
- 【Leetcode_easy】783. Minimum Distance Between BST Nodes
problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...
随机推荐
- dom4j,json,pattern性能对比【原】
报文大概2000字节,对比时为只取其中某个节点的值即可. 以下对比可知取少量节点时pattern性能是远大于dom4j,和json的, 但取大量的时候就不能这么以偏概全了. dom4j和pattern ...
- HP Z620 Windows 7 系统安装(含磁盘阵列)
由于HP Z620 做了Raid 5磁盘阵列,导致安装系统时,系统加载不了磁盘的驱动,无法将系统安装到硬盘上,正确的方法是:下载SATA驱动,在需要加载驱动的地方,利用另一个U盘,“浏览”解压好的驱动 ...
- [物理学与PDEs]第5章第6节 弹性静力学方程组的定解问题
5. 6 弹性静力学方程组的定解问题 5. 6. 1 线性弹性静力学方程组 1. 线性弹性静力学方程组 $$\bee\label{5_6_1_le} -\sum_{j,k,l}a_{ijkl}\cf ...
- 鼠标右键添加Sublime Text
鼠标右键添加Sublime Text 参考 将sublime添加到鼠标右键 实践 1. win+R 输入regedit 2. 输入路径: 计算机\HKEY_CLASSES_ROOT\*\shell\ ...
- 5.CentOS7安装mariadb
MariaDB 和 MySQL 使用是一样的,二者只要安装一个就行了 MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司 ...
- 「CTSC2018」暴力写挂
毫无$ Debug$能力 全世界就我会被卡空间.jpg LOJ #2553 UOJ #400 Luogu P4565 题意 给定两棵树$ T,T'$,求一组点对$ (x,y)$使得$deep(x)+d ...
- Web从入门到放弃<6>
<1> Canvas. 1,灰度图: js: function showAsGray() { var imgNode = document.getElementById('img'); ...
- RequireJS - 个人小入门
quirejs : http://www.requirejs.cn/ 叶小钗 : http://www.cnblogs.com/yexiaochai/p/3214926.html app.js 展示 ...
- Windows下ToroiseSVN基本使用&&在Visual studio中使用SVN
首先在 https://tortoisesvn.net/downloads.html 下载svn客户端 下载并安装好之后再开始菜单会出现如下图标: 现在可以开始使用TortoiseSVN了,选择一个本 ...
- Python 通用爬虫 和讯博客 scrapy
目标站点需求分析 通用爬虫,获取和讯博客所有博文 涉及的库 scrapy,re,requests,mysql URL RULE 解析单页源码 保存到数据库 结果