对BST树进行中序遍历,得到递增序列,然后依次计算相邻两元素之间的差,并保存最小的差。

class Solution {
public:
vector<TreeNode*> V;
void postTree(TreeNode* node)
{
if (node != NULL)
{
if (node->left != NULL)
{
postTree(node->left);
}
V.push_back(node);
if (node->right != NULL)
{
postTree(node->right);
}
}
}
int minDiffInBST(TreeNode* root) {
postTree(root);
int min = INT_MAX;
if (V.size() < )
{
return ;
}
int last = V[]->val;
for (int i = ; i < V.size(); i++)
{
int diff = V[i]->val - last;
if (min > diff)
{
min = diff;
}
last = V[i]->val;
}
return min;
}
};

leetcode783的更多相关文章

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

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

  2. Leetcode783.Minimum Distance Between BST Nodes二叉搜索树结点最小距离

    给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意,root是树结点对象(Tr ...

  3. LeetCode783. 二叉搜索树节点最小距离

    题目 和LeetCode530没什么区别 1 class Solution { 2 public: 3 vector<int>ans; 4 int minDiffInBST(TreeNod ...

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

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

随机推荐

  1. iOS自动化探索(三)WebDriverAgent Python Client

    之前我们在终端试着调用过WDA API, 今天我们在看一个Python封装的api库 https://github.com/openatx/facebook-wda 安装方式(一): pip inst ...

  2. 【第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛-J】 强迫症的序列

    小A是一个中度强迫症患者,每次做数组有关的题目都异常难受,他十分希望数组的每一个元素都一样大,这样子看起来才是最棒的,所以他决定通过一些操作把这个变成一个看起来不难受的数组,但他又想不要和之前的那个数 ...

  3. LeetCode OJ:Remove Nth Node From End of List(倒序移除List中的元素)

    Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...

  4. LeetCode OJ:Integer to Roman(转换整数到罗马字符)

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  5. [转载]Python注册表信息丢失的解决方案

    今天安装Python的模块时,安装失败,提示信息:Python version 2.7 required, which was not found in the registry. 原因在于Pytho ...

  6. 服务升级带来的Bug,BAT也不能幸免

    这是标题党,关于阿里的,BT躺枪了. 为什么淘宝上找不到"亲淘"了? 好吧,我今天遇到了一个Bug: 立即更新,然后你看到了: 才发现亲淘不能使用了. 看官方页面: 提示:2016 ...

  7. Arcgis for Javascript之统计图的实现

    首先,截个图给大家看看效果: 初始化状态 放大后的状态 点击选中后的状态 如上图所示,一般的涉及到的地图的统计涉及到上述所展示的三个状态:1.初始化状态:2.缩放后的状态:3.点击选中显示详情状态.第 ...

  8. 2017年国内已经开设机器人工程专业(080803T)高校名单

    相关资料来源于教育部公布的2014年度和2016年度普通高等院校本科专业备案或审批结果的通知: 2014年批次 http://www.moe.edu.cn/publicfiles/business/h ...

  9. 关于Sublime Text不能在打开方式中显示并且不能被设置成默认打开方式的问题

    解决方法: 1. Windows 输入 regedit 后 回车 打开注册表 2.找到 "HKEY_CLASSES_ROOT\Applications\sublime_text.exe\sh ...

  10. 一起来看CORE源码(一) ConcurrentDictionary

    先贴源码地址 https://github.com/dotnet/corefx/blob/master/src/System.Collections.Concurrent/src/System/Col ...