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 ...
随机推荐
- C# 页面向controller中跳转匹配方法的时候,当controller中有两个重载方法时候,不发生跳转
在ajax中的URL跳向controller一个方法时候,controller中有两个重载的方法,ajax不发生跳转,当删除另外一个方法之后,正常跳转. 不知道,是我自己写的有问题,还是control ...
- python:django
====启动django==== python manager.py runserver --host 0.0.0.0 --port 9008 python manager.py runserver ...
- ubuntu系列-安装google浏览器
转载:http://www.linuxidc.com/Linux/2013-10/91857.htm 对于刚刚开始使用Ubuntu并想安装谷歌Chrome浏览器的新用户来说,本文所介绍的方法是最快捷的 ...
- django的model操作整理
单表操作 增加数据 auther_obj = {"auther_name":"崔皓然","auther_age":1} models.aut ...
- swift - 3D 视图,截图,关键字搜索
1.xib 上的 3D效果 按钮 2. import UIKit //1.导入框架 import MapKit class ViewController: UIViewController { @IB ...
- python3.x:No matching distribution found for PIL
安装完成即可解决无法引入PIL的问题.
- <history> 特别报道:Google离职富翁们都在干什么?
特别报道:Google离职富翁们都在干什么? 时间:2008-01-23 10:16:47作者:CNET科技资讯网 本文关键词:Google CNET科技资讯网1月23日国际报道 假如你拥有1千万或1 ...
- Android 工程目录
app java:我们写Java代码的地方,业务功能都在这里实现 res:存放我们各种资源文件的地方,有图片,字符串,动画,音频等,还有各种形式的XML文件 Gradle Scripts 1.res资 ...
- 25.mysql中的常用工具
25.mysql中的常用工具25.1 mysql客户端连接工具跳转至mysql安装目录下的bincd C:\Program Files\MySQL\MySQL Server 5.7\binmac下cd ...
- 24.Mysql高级安装和升级
24.Mysql高级安装和升级24.1 Linux/Unix平台下的安装 24.1.1 安装包比较Linux下的Mysql安装包分为RPM包.二进制包.源码包3种.RPM包优点是安装简单,适合初学者: ...