783. Minimum Distance Between BST Node
方法一,非递归方法,中序遍历
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution
{
public: int minDiffInBST(TreeNode* root)
{
stack<TreeNode*>s;
int mindiff=INT_MAX;
TreeNode *p=root;
int pre=-(root->val),cur=;
while(p||!s.empty())
{
if(p)
{
s.push(p);
p=p->left;
}
else
{
p=s.top();
s.pop();
cur=p->val;
mindiff=min(mindiff,cur-pre);
pre=cur;
p=p->right;
}
}
return mindiff;
}
};
方法二,递归方法,中序遍历
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/ static int wing=[]()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return ;
}(); class Solution {
int dif=INT_MAX, val=-;
public:
int minDiffInBST(TreeNode* root)
{
if(root->left!=nullptr)
minDiffInBST(root->left);
if(val>=) dif=min(dif,root->val-val);
val=root->val;
if(root->right!=nullptr)
minDiffInBST(root->right);
return dif;
} };
简单,问题不大
783. Minimum Distance Between BST Node的更多相关文章
- 【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 ...
- 783. Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- 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】783. Minimum Distance Between BST Nodes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 日期 题目地址:https://leetc ...
- LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)
783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...
- 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 ...
随机推荐
- Error in building opencv with ffmpeg
I installed ffmpeg according to this article. ffmpeg installation was ok. Now I build opencv with ff ...
- TOJ1698/POJ3264Balanced Lineup (线段树 or RMQ-ST)
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=1698 时间限制(普通/Java): ...
- python常见的数据结构
https://www.cnblogs.com/5poi/p/7466760.html
- SpringCloud如何创建一个服务提供者provider
SpringCloud如何创建一个服务提供者provider 创建子moudle provider-demo 创建一个子module,项目名叫provider-demo. 填充springboot和s ...
- rosrun和roslaunch
rosrun allows you to run an executable(可执行) in an arbitrary(任意) package without having to cd (or ros ...
- three.map.control
网址:https://github.com/anvaka/three.map.control 在threejs群里发现的一个很有意思的问题之前没有接触过: 存在的问题: 我在微信小游戏中,用orbi ...
- istream_iterator和ostream_iterator
总结: istream_iterator<T>in(strm);T指明此istream_iterator的输入类型,strm为istream_iterator指向的流 提供了输入操作符(& ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
- Get、Post 提交的乱码问题
1.问题 在spring mvc开发的时候出现乱码问题: 2.解决方案 (1)Get提交:tomcat容器接收的造成的乱码问题,修改server.xml文件: (2)Post提交:在web.xml中配 ...
- waf相关
google上搜索下面信息: waf site:klionsec.github.io 有几篇比较有意思的问题: https://klionsec.github.io/2017/07/09/nginx- ...