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

 /**
* 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. linux下的C++项目创建

    CMake项目的完整构建 Linux下的CMake项目通常由几个文件夹组成.小伙伴们可以先在自己的电脑上新建一个文件夹,作为你代码的根目录,然后往里面建几个子文件夹,这里并不涉及具体的代码,只是可以作 ...

  2. JavaScript各种继承方式(二):借用构造函数继承(constructor stealing)

    一 原理 在子类的构造函数中,通过call ( ) 或 apply ( ) 的形式,调用父类的构造函数来实现继承. function Fruit(name){ this.name = name; th ...

  3. synchronized细节问题(二)

    使用synchronized声明的方法在某些情况下是有弊端的,比如A线程调用同步的方法执行一个很长时间的任务,那么B线程就必须等待比较长的时间才能执行,这样的情况下,可以使用synchronized代 ...

  4. java中Date无法获取数据库时分秒的问题

      数据库使用的字段是timestamp(6),在数据库看的时候明明时分秒是有的,然而通过rs.getDate()获取出来的时候时分秒就没有了,查了一下资料终于解决了,这里有一个重要的知识点,java ...

  5. ATM取款机系统代码及此次作业感受

    本次乃我们软件工程专业开学第一个小测试,本来以为是和之前2016级相同的或者类似得软件,所以之前学了好久的那个程序完全失去了它的作用,当然了老师也从来没有按套路出过牌,所以这个下马威我觉得作用起到了. ...

  6. swift - 画图截取图片 - 保存相册

    1.图片截取 func test(addView:UIView) -> UIImage?{ UIGraphicsBeginImageContextWithOptions(CGSize(width ...

  7. jq里面关于disable的用法

    //两种方法设置disabled属性$('#areaSelect').attr("disabled",true);$('#areaSelect').attr("disab ...

  8. 安装后没有IP

    修改  /etc/sysconfig/network-scripts/ifcfg-eth0 改为yes

  9. ROS launch总结

    1 运行Launch文件2 新建Launch文件3  在namespace中启动nodes 4   remapping names 5 其他的launch元素 1 运行Launch文件 Launch文 ...

  10. go语言使用go-sciter创建桌面应用(二) ui元素查找,增加,删除,修改

    我们可以通过go-sciter给我们提供的方法,方便的对html,css编写的UI界面进行增删改查. demo3.go代码如下: package main; import ( "github ...