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 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.
思路:bst树的中序遍历是一个升序的数组,那么中序遍历一遍,放到vector中,相邻的做差比较就好了。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
priority_queue<int> q;
void dfs(TreeNode* root) {
if (root == nullptr) return;
q.push(root->val);
dfs(root->left);
dfs(root->right);
}
int minDiffInBST(TreeNode* root) {
dfs(root);
int x = q.top();q.pop();
int ans = 10000000;
while(!q.empty()) {
int y = q.top();q.pop();
ans = min (x-y, ans);
x = y;
}
return ans;
}
};
leetcode leetcode 783. Minimum Distance Between BST Nodes的更多相关文章
- 【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 日期 题目地址:https://leetc ...
- 【Leetcode_easy】783. Minimum Distance Between BST Nodes
problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...
- [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 ...
- 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算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)
这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ...
- 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 ...
- [Swift]LeetCode783. 二叉搜索树结点最小距离 | Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
随机推荐
- 微信关注事件bug记录
年前研究了一下微信带参数的二维码,处理邀请注册成会员等的方式 通过带参数的二维码触发微信的 subscribe(订阅) 或者 SCAN (已经订阅后)事件,然后抓取eventKey(记录邀请人的信息 ...
- electron入门教程
1.atom/electron github: https://github.com/atom/electron 中文文档: https://github.com/atom/electron/tree ...
- 【linux】CentOS编译程序报错 修复 ./Modules/_ssl.c:64:25: 致命错误:openssl/rsa.h:没有那个文件或目录
如果你在编译时遇到这个错误,这可能是下面的原因:你尝试编译的程序使用OpenSSL,但是需要和OpenSSL链接的文件(库和头文件)在你Linux平台上缺少. 所以在CentOS下, 退到根路径,[需 ...
- 45个非常有用的Oracle查询语句(转自开源中国社区)
日期/时间 相关查询 获取当前月份的第一天 运行这个命令能快速返回当前月份的第一天.你可以用任何的日期值替换 “SYSDATE”来指定查询的日期. SELECT TRUNC (SYSDATE, 'MO ...
- c语言函数---I
函数名: imagesize 功 能: 返回保存位图像所需的字节数 用 法: unsigned far imagesize(int left, int top, int right, int bott ...
- 21. Spring Boot过滤器、监听器【从零开始学Spring Boot】
转载:http://blog.csdn.net/linxingliang/article/details/52069490 上一篇文章已经对定义Servlet 的方法进行了说明,过滤器(Filter) ...
- Cent OS下发送邮件
首先安装发送邮件的服务: yum install -y sendmail 安装完成之后在安装mutt yum install -y mutt 安装完成之后我们就可以发送邮件了 mutt tes ...
- windows ffmpeg 推送摄像头数据到rtmp服务
文本主要讲述windows系统下如何利用ffmpeg获取摄像机流并推送到rtmp服务,命令的用法前文 中有讲到过,这次是通过代码来实现.实现该项功能的基本流程如下: 图1 ffmpeg推流流程图 较前 ...
- 【每日Scrum】第八天(4.29) TD学生助手Sprint2
站立会议 组员 今天 签到 刘铸辉 (组长) 绩效考核 Y 刘静 测试用例书写 测试bug报告 测试详细报告 Y 解凤娇 Y 王洪叶 项目可行性报告 项目开发计划书 需求分析(已完成并发布) Y 胡宝 ...
- *Android 多线程下载 仿下载助手(改进版)
首先声明一点: 这里的多线程下载 并非指的 多个线程下载一个 文件.而是 每一个线程 负责一个文件. 真正的多线程 希望后面能给大家带来. ------------- 欢迎 爱学习的小伙伴 加群 ...