方法一,非递归方法,中序遍历

 /**
* 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的更多相关文章

  1. 【Leetcode_easy】783. Minimum Distance Between BST Nodes

    problem 783. Minimum Distance Between BST Nodes 参考 1. Leetcode_easy_783. Minimum Distance Between BS ...

  2. [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 ...

  3. 783. Minimum Distance Between BST Nodes

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

  4. 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 ...

  5. 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 ...

  6. 【LeetCode】783. Minimum Distance Between BST Nodes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 中序遍历 日期 题目地址:https://leetc ...

  7. LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)

    783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...

  8. LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)

    这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ...

  9. [Swift]LeetCode783. 二叉搜索树结点最小距离 | Minimum Distance Between BST Nodes

    Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...

随机推荐

  1. unity3d英语单词拼写小游戏Pics Quiz Maker With Categories 3.0

    下载地址: https://item.taobao.com/item.htm?spm=0.7095261.0.0.19f71debcef4hT&id=575991216080

  2. width多少,超过了用....表示

    maxWidth:'140px',whiteSpace:'nowrap',overflow:'hidden',textOverflow:'ellipsis'

  3. Efficiency optimizing

    *low efficiency l_it_alv_stpox[] = g_it_alv_stpox[]. SORT l_it_alv_stpox BY zz_matnr idnrk. LOOP AT ...

  4. linux 备忘录

    1. ps aux|grep 程序 -------->查看当前程序是否运行 ps aux|grep nginx 2. tar -zxvf 压缩包 ---------> 解压缩 tar -z ...

  5. Head First Servlets & JSP 学习笔记 第三章 —— MVC迷你教程

    临渊羡鱼,不如退而结网!是时候动手搞事情了! 我们的四大步骤: ①分析用户的视图(也就是浏览器要显示的东西),以及高层体系结构: (这个就是所谓的前端吧?用JSP?JSP可以当成Html来用吧?高层体 ...

  6. 比特币运行原理[z]

    https://baijiahao.baidu.com/s?id=1581755535769652543&wfr=spider&for=pc 这篇文章主要讲解比特币是什么?它的运行原理 ...

  7. oracle 直接复制表内容到新表

    不知道为什么,刚建的oracle数据库删除数据很慢,表里面有120多万数据,非常地慢 于是采用的复制的方法,命令如下: create table students_backup as select * ...

  8. (转)在WCF服务的ServiceReferences.ClientConfig中使用相对路径

    问题: Silverlight项目中添加服务引用后会在Silverlight项目中生成一个ServiceReferences.ClientConfig文件,这个文件中包含了引用服务的绑定(bindin ...

  9. XStream将XML转javaben,出现多余的tag,导致出错

    今天在测试银联无卡快捷支付的案例时,多了一个多tag兼容性测试,它是指银联的XML报文中会出现多余的tag,如果我们用XStream解析的时候,没有Javabean的字段可以对应上,就会报错!提示: ...

  10. JSP概述、API、注释

    JSP自带的API包含4个包,可通过Tomcat的官网查看,JSP和EL的API是分开的 javax.servlet.jsp // 包含用于Servlet/JSP容器将JSP页面翻译为Servlet的 ...