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.

Note:

  1. The size of the BST will be between 2 and 100.
  2. The BST is always valid, each node's value is an integer, and each node's value is different.

思路:

遍历二叉树,将元素值排序,最小的差肯定出现在相邻两个元素里,遍历数组即可。

 void preorderTree(TreeNode* root,vector<int>& vc)
{
if(root ==NULL) return ;
vc.push_back(root->val);
preorderTree(root->left,vc);
preorderTree(root->right,vc);
}
int minDiffInBST(TreeNode* root)
{
vector<int>vc;
preorderTree(root,vc);
sort(vc.begin(),vc.end());
int t =INT_MAX;
for(int i=;i<vc.size()-;i++)
{
t = min(t,vc[i+] - vc[i]);
}
return t;
}

[leetcode-783-Minimum Distance Between BST Nodes]的更多相关文章

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

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

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

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

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

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

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

  6. 783. Minimum Distance Between BST Nodes

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

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

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

  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),返回树中任何两个 ...

  10. [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. 【js】中的小技巧

    本文主要介绍一些JS中用到的小技巧 1. 类型强制转换   1.1 string强制转换为数字 可以用*1来转化为数字(实际上是调用.valueOf方法) 然后使用Number.isNaN来判断是否为 ...

  2. 卡常三连(快读快写+re)

    快读: inline int in() { char ch; ; '))); a*=;a+=ch-'; ,a+=ch-'; return a; } 快写: inline void out(int a) ...

  3. python中的extend和append

    list.append(object) 向列表中添加一个对象object list.extend(sequence)  把一个序列seq的内容添加到列表中 old = ['a', 'b'] new = ...

  4. Spring的扩展

    Spring中引用属性文件 JNDI数据源 Spring中Bean的作用域 Spring自动装配 缺点

  5. IE8 如何 不显示 选项 卡,直接在任务显示 各个页面?

    IE8  如何 不显示 选项 卡,直接在任务显示 各个页面? 在  工具->Internet 选项(o) ->常规--选项卡-设置->把第一个勾去掉  “启用选项卡浏览(需要重新启动 ...

  6. QEP之init()和dispatch()流程图

    抽象状态机类QFsm或QHsm有一个函数指针,用于在继承的具体状态机类中指向具体的状态函数,其有两个对外的接口函数init()和dispatch(),其工作原理是理解状态机处理事件过程的关键. 具体状 ...

  7. django路由基本使用-6

    路由定义位置 django的路由是定义在 urls.py 文件下的 urlpatterns 列表中的. urls.py 文件是路由解析的入口. from django.conf.urls import ...

  8. 『Python基础-1 』 编程语言Python的基础背景知识

    #『Python基础-1 』 编程语言Python的基础背景知识 目录: 1.编程语言 1.1 什么是编程语言 1.2 编程语言的种类 1.3 常见的编程语言 1.4 编译型语言和解释型语言的对比 2 ...

  9. bit_length

    #当十进制用二进制表示时,最少使用的位数 v=2data=v.bit_length()print(data)

  10. 4-c++教程起航篇-学习笔记

    c++教程起航篇 我们会讲C++那些事,C++与C语言的关系. C++诞生于贝尔实验室. C++之父: 本贾尼·斯特劳斯特卢普 C++社区排行榜 最新排行,c++排名第三,Python排名第四 C++ ...