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. 求二叉搜索树任意两节点之间的差值,要求最小 C++(4ms):
 /**
* 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:
int res = INT_MAX ;
int pre = - ;
int minDiffInBST(TreeNode* root) {
if (root->left != NULL)
minDiffInBST(root->left) ;
if (pre >= )
res = min(res , root->val - pre) ;
pre = root->val ;
if (root->right != NULL)
minDiffInBST(root->right) ;
return res ;
}
};
 

783. Minimum Distance Between BST Nodes的更多相关文章

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

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

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

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

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

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

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

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

  8. [LeetCode] Minimum Distance Between BST Nodes 二叉搜索树中结点的最小距离

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

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

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

随机推荐

  1. 【原创】【2】rich editor系列教程。了解document.execommand操作,保存丢失的range,实时反馈样式给工具栏

    [原创][2]rich editor系列教程.了解document.execommand操作,保存丢失的range,实时反馈样式给工具栏 索引目录:http://www.cnblogs.com/hen ...

  2. golang字符串常用函数

    package utils import "fmt" import "strconv" import "strings" var str s ...

  3. HDU--1010

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1010 分析:dfs+奇偶剪枝.   Tempter of the Bone #include<c ...

  4. VS集成opencv编译C++项目遇到的问题

    当我们新建一个c++项目的时候总是提示脚本错误的信息,虽然不影响使用,但是还是很烦躁,对于有强迫症的我来说,实在受不了,终于找到了解决方案 这个提示的路径根据大家自己安装vs的路径来查找: http: ...

  5. $('.goods_tag_ids_all')[0].checked = true;//~~~~~ 单条改变checkbox 属性样式

    //点击左边全选选中时,都全选$('.goods_tag_ids_all').on('click',function(){ if($('.goods_tag_ids_all').is(':checke ...

  6. 「Django」学习之路,持续更改

    一.setting设置 1.设置 局域网可以部署连接 ALLOWED_HOSTS = ['*.besttome.com','192.168.1.100'] 2.static配置 STATIC_URL ...

  7. Java中String的替换函数:replace与replaceAll的区别

    例如有如下x的字符串  String x = "[kllkklk\\kk\\kllkk]"; 要将里面的"kk"替换为++,可以使用两种方法得到相同的结果  r ...

  8. uva 557 Burger

    https://vjudge.net/problem/UVA-557 题意: n个人,n/2个牛肉煲,n/2个鸡肉堡 每次抛硬币,根据正反决定每个人吃什么汉堡 如果某一个汉堡被选完了,就不抛了 问最后 ...

  9. Codeforces 221 A. Little Elephant and Function

    A. Little Elephant and Function time limit per test 2 seconds memory limit per test 256 megabytes in ...

  10. 拓扑排序 最大字典序+优先队列 BZOJ 4010

    http://www.lydsy.com/JudgeOnline/problem.php?id=4010 4010: [HNOI2015]菜肴制作 Time Limit: 5 Sec  Memory ...