[leetcode-783-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.
思路:
遍历二叉树,将元素值排序,最小的差肯定出现在相邻两个元素里,遍历数组即可。
void preorderTree(TreeNode* root,vector<int>& vc)
{
if(root ==NULL) return ;
vc.push_back(root->val);
preorderTree(root->left,vc);
preorderTree(root->right,vc);
}
int minDiffInBST(TreeNode* root)
{
vector<int>vc;
preorderTree(root,vc);
sort(vc.begin(),vc.end());
int t =INT_MAX;
for(int i=;i<vc.size()-;i++)
{
t = min(t,vc[i+] - vc[i]);
}
return t;
}
[leetcode-783-Minimum Distance Between BST Nodes]的更多相关文章
- leetcode leetcode 783. Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- 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 ...
- 【Leetcode_easy】783. Minimum Distance Between BST Nodes
problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...
- 【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 日期 题目地址:https://leetc ...
- [LeetCode&Python] Problem 783. Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- 783. Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)
783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...
- [LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)
这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ...
- [Swift]LeetCode783. 二叉搜索树结点最小距离 | Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
随机推荐
- Centos7验证Kickstart文件是否完整方法
1.1 功能简介 CentOS 7 包含 ksvalidator 命令行程序,可使用该程序进行确认Kickstart文件.这个工具是 pykickstart 软件包的一部分.要安装pykicks ...
- 帝国cms全文搜索 增加自定义字段搜索
帝国cms全站搜索功能只能调出固定的几个字段,如果想搜索其他字段的值,这时我们应该怎么办呢?开拓族网站有这个需求,所以研究了一下帝国的全站搜索,后来发现在/e/sch/index.php中可以直接对数 ...
- 视频网站数据MapReduce清洗及Hive数据分析
一.需求描述 利用MapReduce清洗视频网站的原数据,用Hive统计出各种TopN常规指标: 视频观看数 Top10 视频类别热度 Top10 视频观看数 Top20 所属类别包含这 Top20 ...
- Python系列之入门篇——pytables及其客户端
pytables及其客户端查看 pytables # ubuntu sudo apt-get install python-tables pip install flask flask-httpaut ...
- 隐藏Windows不常用设置项
Windows10的设置里面有很多我们不想看见的项目,例如"轻松使用","隐私","游戏","Cortana"等,我们可 ...
- linux3.4.2之DMA驱动完整程序
/* *参考arch/arm/mach-s3c24xx/dma-s3c2410.c */ #include <linux/module.h> #include <linux/kern ...
- 20190129-‘abcdefgh’里面挑出3个字母进行组合,一共有多少组合
一. 百度面试题‘abcdefgh’里面挑出3个字母进行组合,一共有多少组合,要求3个字母中不能有重复的组合,三个字母同时出现的次数只能出现一次,如出现了abc就不能出现cab,bca等 思路: 1. ...
- PAT A1060 (Advanced Level) Practice
If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered ...
- java 递归打印20个斐波那契数
class Test { public static void main(String[] args) { // feibo j=new feibo(); for (int n = 1; n < ...
- 利用谷歌翻译网站和Adobe Acrobat翻译英文文档,且鼠标放置后显示英文原文(无字数限制)(18/12/11更新)
软件:Adobe Acrobat 网页:https://translate.google.cn/?tr=f&hl=zh-CN 方法: 第一步:用Adobe Acrobat 打开英文 ...