leetcode783
对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的更多相关文章
- [Swift]LeetCode783. 二叉搜索树结点最小距离 | Minimum Distance Between BST Nodes
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the ...
- Leetcode783.Minimum Distance Between BST Nodes二叉搜索树结点最小距离
给定一个二叉搜索树的根结点 root, 返回树中任意两节点的差的最小值. 示例: 输入: root = [4,2,6,1,3,null,null] 输出: 1 解释: 注意,root是树结点对象(Tr ...
- LeetCode783. 二叉搜索树节点最小距离
题目 和LeetCode530没什么区别 1 class Solution { 2 public: 3 vector<int>ans; 4 int minDiffInBST(TreeNod ...
- LeetCode 783. 二叉搜索树结点最小距离(Minimum Distance Between BST Nodes)
783. 二叉搜索树结点最小距离 LeetCode783. Minimum Distance Between BST Nodes 题目描述 给定一个二叉搜索树的根结点 root, 返回树中任意两节点的 ...
随机推荐
- Superset 初探
安装都是借鉴的别人的,已经剪裁下来.到自己文件夹里了. 下面介绍.如何启动superset ,BI 分析工具.这是我以前的强项.应该没问题. 问题: 安装好了之后,再打开localhost 就拒绝访问 ...
- 一个html+js+ashx+easyui+ado.net权限管理系统
http://www.cnblogs.com/oppoic/p/html_js_ashx_easyui_authorize.html
- 配置 Web 组件服务器 IIS 证书
用 IIS 6 配置 Web 组件证书(对于 Windows Server 2003) 使用 IIS 管理器向 Web 组件服务器分配证书.对合并池配置中的 Standard Edition ...
- 利用 innodb_force_recovery 解决MySQL服务器crash无法重启问题
背景 MySQL服务器因为磁盘阵列损坏机器crash,重启MySQL服务时 报如下错误: InnoDB: Reading tablespace information from the .i ...
- python基础之递归,声明式编程,面向对象(一)
在函数内部,可以调用其他函数,如果一个函数在内部调用自身本身,这个函数就是递归函数.递归效率低,需要在进入下一次递归时保留当前的状态,解决方法是尾递归,即在函数的最后一步(而非最后一行)调用自己,但是 ...
- react-redux: counter
store: import {createStore,applyMiddleware, compose} from "redux"; import thunk from " ...
- QGrapicsItem类
这个类翻译了好久,实在是成员函数太多了,分享出来,希望对大家有用,多多支持哦~~ 详细介绍 QGraphicsItem类是视图框架的一部分,是在一个QGraphicsScene中最基本的图形类,它为绘 ...
- ng 双向数据绑定 实现 注册协议效果
效果: 代码: <!DOCTYPE html> <html ng-app="myApp"> <head lang="en"> ...
- Compile For Cydia Submission Author: BigBoss Updated September 23, 2011
Compile For Cydia Submission Author: BigBoss Updated September 23, 2011: In order to submit your app ...
- GO语言打包ICO图标
1. go get github.com/akavel/rsrc2. 创建manifest文件, 命名:main.exe.manifest : <?xml version="1.0&q ...