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 ...
随机推荐
- rancher安装,快速安装
apt-get install docker.io docker -y docker run -d --restart=always -v /data/rancher_server:/var/lib/ ...
- elasticsearch6.5.x-centos6
elasticsearch6.5.x-centos6 elasticsearch 和 关系型数据库中的类比 es ====== RDBMS index ----- database type ---- ...
- [日常摸鱼][poj2777]Count Color-线段树
辣鸡会考考完啦哈哈哈哈 题意:一块板分成$L$块,每次给一段连续的块染色或者询问一段有几种颜色,颜色的范围$\leq 30$ 我记得我好像做过一个类似的二维染色的问题-不过那个用树状数组直接过掉了- ...
- 基于Python PIL实现简单图片格式转化器
基于Python PIL实现简单图片格式转化器 目录 基于Python PIL实现简单图片格式转化器 1.简介 2.前期资料准备 2.1.1如何实现图片格式转换? 2.1.2如何保存需要大小的图片? ...
- Spark的RPC
Spark 的 RPC 什么是RPC 在Spark中很多地方都涉及网络通信,比如Spark各个组件间的消息互通.用户文件与Jar包的上传.节 点间的Shuffle过程.Block数据的复制与备份等. ...
- 在GitHub里面如何删除库
GitHub是一个面向开源及私有软件项目的托管平台,今天将为大家介绍如何在GitHub中彻底删除一个代码仓库. 在GitHub里面如何删除库 1.先进入个人仓库里面 2.点击进入你想要的删除的库 3. ...
- java中token的生成和验证
package com.zjn.token; /** * token编码工具类 * @author ouyangjun */ public class TokenEncryptUtils { // 编 ...
- sql语句查询,limit与order by 同时出现,应该order by在前面
eg:select orderid,status,createtime from orders where appid = :appId and userid = :userId order by c ...
- synchronized关键字jvm实现及各种锁
一.synchronized的字节码执行过程 在java语言中存在两种内建的synchronized语法:1.synchronized语句:2.synchronized方法. 对于synchroniz ...
- Alpha冲刺——代码规范与计划
这个作业属于哪个课程 https://edu.cnblogs.com/campus/fzzcxy/2018SE1 这个作业要求在哪里 https://edu.cnblogs.com/campus/fz ...