【easy】530. Minimum Absolute Difference in BST
找BST树中节点之间的最小差值。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//BST树中序遍历就是递增的序列,相邻两个数的差值,找最小
class Solution {
public:
int min_res = INT_MAX;
TreeNode* pre; int getMinimumDifference(TreeNode* root) {
helper(root);
return min_res;
} void helper(TreeNode*root){
if (!root)
return;
helper(root->left); //1 if (pre)
min_res = min(min_res, abs(root->val - pre->val));
pre = root; helper(root->right); //2
}
};
【easy】530. Minimum Absolute Difference in BST的更多相关文章
- 【leetcode_easy】530. Minimum Absolute Difference in BST
problem 530. Minimum Absolute Difference in BST 参考 1. Leetcode_easy_530. Minimum Absolute Difference ...
- 【LeetCode】530. Minimum Absolute Difference in BST 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 51. leetcode 530. Minimum Absolute Difference in BST
530. Minimum Absolute Difference in BST Given a binary search tree with non-negative values, find th ...
- 【leetcode】1200. Minimum Absolute Difference
题目如下: Given an array of distinct integers arr, find all pairs of elements with the minimum absolute ...
- 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- LeetCode 530. Minimum Absolute Difference in BST (二叉搜索树中最小绝对差)
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- [LeetCode&Python] Problem 530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
[抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...
随机推荐
- asp.net 文件分片上传
最近在研究文件上传,里面的门道还是挺多的,网上大多数文章比较杂乱,代码都是片段,对于新手小白来说难度较高,所以在此详细写一下今天看到的一个demo,关于文件分片上传的. <!DOCTYPE ht ...
- MySQL-悲观锁和乐观锁
引言 悲观锁和乐观锁指的并不是一种锁,而是一种思想,一种并发控制的方法. 在事务并发执行的情景中,可能存在多个用户同时更新同一条数据的情况,这可能会产生冲突导致丢失更新或者脏读. 丢失更新是指一个事 ...
- 虚拟机网络连接方式导致的p地址为10.0.2.*的问题
全世界都知道通过 ifconfig 命令查看本机ip地址,我当然希望安装的虚拟机和当前局域网的其他机器一样内网ip为192.168.1.*,如下图所示: 而当我执行该命令时,实际情况却是这样的: 解决 ...
- RabbitMQ消息队列
RabbitMQ消息队列 !!! 注意,保证服务器的内存足够,磁盘足够,以及删除/etc/hosts中没有用的dns解析 # 优点,能够保证消息数据持久化,不丢失,支持高并发 安装学习rabbitm ...
- MT【324】增量代换
实数$a,b,c$满足$a^2+b^2+c^2=1$求$f=\min\{(a-b)^2,(b-c)^2,(c-a)^2\}$的最大值 分析:由对称性不妨设$c\ge b\ge a$,令$b-a=s,c ...
- EditTextUtil 监听输入字数
package com.toge.ta.utils; import android.text.Editable;import android.text.Selection;import android ...
- 在SpringBoot项目中添加logback的MDC
在SpringBoot项目中添加logback的MDC 先看下MDC是什么 Mapped Diagnostic Context,用于打LOG时跟踪一个“会话“.一个”事务“.举例,有一个web ...
- 第七周博客作业<西北师范大学|李晓婷>
1.助教博客链接:https://home.cnblogs.com/u/lxt-/ 2.本周应批作业0,实批作业0. 3.本周小结:本周我们助教开始准备团队项目题目,下周三之前将会进行作业提交.
- (九) 主机增加打印(串口+ssh)
目录 主机增加打印(串口+ssh) ssh 串口打印 title: 主机增加打印(串口+ssh) date: 2019/4/23 20:10:00 toc: true --- 主机增加打印(串口+ss ...
- 在线批量修改mysql中表结构
在线批量修改mysql中表结构 1.获取要修改的表的表名称登录mysql库,查询出所有表 show tables; 将需要修改表结构的表名称存放到b.txt文件中2.执行修改修改表引擎为InnoDB ...