LeetCode530. 二叉搜索树的最小绝对差
题目
又是常见的BST,要利用BST的性质,即中序遍历是有序递增序列。
法一、中序遍历
1 class Solution {
2 public:
3 vector<int>res;
4 void InOrder(TreeNode* p){
5 if(p!=NULL){
6 InOrder(p->left);
7 res.push_back(p->val);
8 InOrder(p->right);
9 }
10 }
11 int getMinimumDifference(TreeNode* root) {
12 InOrder(root);
13 int min = INT_MAX;
14 for(int i = 0;i < res.size() - 1;i++){
15 if(abs(res[i]-res[i+1]) < min )
16 min = abs(res[i]-res[i+1]);
17 }
18 return min;
19 }
20
21 };
法二、优化后的中序遍历,不开数组,在递归过程中应用pre指针保存前结点
1 class Solution {
2 public:
3 int res = INT_MAX;
4 TreeNode* pre;
5 void InOrder(TreeNode* root){
6 if(root!= NULL) {
7 InOrder(root->left);
8 if(pre!=NULL) res = min(res,root->val - pre->val);
9 pre = root;
10 InOrder(root->right);
11 }
12 }
13
14 int getMinimumDifference(TreeNode* root) {
15 InOrder(root);
16 return res;
17 }
18
19 };
总结:BST题目常利用中序遍历和双指针技巧
LeetCode530. 二叉搜索树的最小绝对差的更多相关文章
- [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between va ...
- Java实现 LeetCode 530 二叉搜索树的最小绝对差(遍历树)
530. 二叉搜索树的最小绝对差 给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值. 示例: 输入: 1 \ 3 / 2 输出: 1 解释: 最小绝对差为 1,其中 2 ...
- [LeetCode]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)
题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ...
- Leetcode:530. 二叉搜索树的最小绝对差
Leetcode:530. 二叉搜索树的最小绝对差 Leetcode:530. 二叉搜索树的最小绝对差 Talk is cheap . Show me the code . /** * Definit ...
- [LeetCode] 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 二叉搜索树的最小绝对差
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入: 1 \ 3 / 2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...
- [LC]530题 二叉搜索树的最小绝对差
①题目 给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值. 示例 : 输入: 1 \ 3 / 2 输出:1 解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- [LeetCode] 235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- 查看kafka topic的消息offset范围
查看各个patition消息的最大位移 sh kafka-run-class.sh kafka.tools.GetOffsetShell --topic mytopic --time -1 --bro ...
- ssl证书---验证等级分类
DV SSL证书(domain Validation SSL): 指只验证网站域名所有权的简易型SSL证书,此类证书仅能起到网站机密信息加密的作用,无法向用户证明网站的真实身份.所以,不推荐在电子商务 ...
- Python实现多个pdf文件合并
背景 由于工作原因,经常需要将多个pdf文件合并后打印,有时候上网找免费合并工具比较麻烦(公司内网不能访问公网),于是决定搞个小工具. 具体实现 需要安装 PyPDF2 pip install PyP ...
- 【故障公告】redis内存耗尽造成博客后台无法保存
非常抱歉,今天上午11:00~11:30左右,由于 redis 服务器内存耗尽造成博客后台故障--保存博文时总是提示"请求太过频繁,请稍后再试",由此给您带来麻烦,请您谅解. 由于 ...
- 什么是泛型?,Set集合,TreeSet集合自然排序和比较器排序,数据结构-二叉树,数据结构-平衡二叉树
==知识点== 1.泛型 2.Set集合 3.TreeSet 4.数据结构-二叉树 5.数据结构-平衡二叉树 ==用到的单词== 1.element[ˈelɪmənt] 要素 元素(软) 2.key[ ...
- Vue2+Koa2+Typescript前后端框架教程--04班级管理示例(路由调用控制器)
上篇文章分享了简单的三层模式和基础文件夹的创建,本篇将以示例的形式详细具体的展示Router.Controller.Service.Model之间业务处理和数据传输. 1. 班级管理数据模型创建.数据 ...
- Cannot assign requested address 和 SO_REUSEADDR 参数
1. 错误 今天项目中出现了大量的java.net.ConnectException: Cannot assign requested address (connect failed) 错误. 刚开始 ...
- 【代码周边】MongoDB与Mysql对比以及插入稳定性分析(指定主键的影响)
在数据库存放的数据中,有一种特殊的键值叫做主键,它用于惟一地标识表中的某一条记录.也就是说,一个表不能有多个主键,并且主键不能为空值. 无论是MongoDB还是MySQL,都存在着主键的定义. 对于M ...
- 有两张表;使用SQL查询,查询所有的客户订单日期最新的前五条订单记录。
客户信息表(c CUSTOM)有以下字段:id.name.mobile 客户订单表(C_ORDER)有以下字段:id.custom_id.commodity.count.order _date Sel ...
- 学习 Gin 总结(2020.12.30-31)
2020.12.30 问题总结 中间件 context.Next() 源码注释: // Next should be used only inside middleware. // It execut ...