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 ...
随机推荐
- 【Pyhton 】 装饰器
# -*- coding:utf8 -*-# Author : Mr·Yang''' 装饰器,带参数装饰器,装饰器中带参数''' import time# 普通装饰器'''def time_decor ...
- mysql 5.7.26 忘记root密码
1.关闭mysql [root@mysql ~]# /etc/init.d/mysqld stopShutting down MySQL.. SUCCESS! 2.修改参数文件/etc/my.cnf ...
- rman catalog配置
1.创建表空间 create tablespace rman_tbs datafile '/u01/app/oracle/oradata/PROD1/rman_tbs01.dbf' size 200m ...
- gitlab+jenkins 持续部署自动化测试
背景:为了减少测试部署时间和减少不必要的重复工作,采用持续集成的方式进行部署,当gitlab的release (测试)分支有代码变动时,自动拉取代码部署测试环境,并进行接口回归测试 优点:部署自动化, ...
- 字节跳动内部微服务架构-Docker实战学习笔记分享 真香
前言 基于 Spring Cloud 的微服务设计和开发,已经越来越多地得到了更多企业的推广和应用,而 Spring Cloud 社区也在不断的迅速发展壮大之中,近几年时间,Spring Cloud ...
- Boost.JSON Boost的JSON解析库(1.75首发)
目录 目录 Boost的1.75版本新库 JSON库简介 JSON的简单使用 编码 最通用的方法 使用std::initializer_list json对象的输出 两种对比 解码 简单的解码 增加错 ...
- kali没有tcptraceroute如何安装
问题描述 尝试使用kali进行路由信息的收集,发现kali没有自带tcptraceroute.在网上搜索教程发现都是Linux下安装,且都是使用yum安装,看了一下发现kali用的也不是yum 这就很 ...
- 单机编排之Docker Compose
当在宿主机启动较多的容器时候,如果都是手动操作会觉得比较麻烦而且容器出错,这个时候推荐使用docker 单机编排工具docker compose,Docker Compose 是docker容器的一种 ...
- 基于frp的内网穿透实例4-为本地的web服务实现HTTPS访问
原文地址:https://wuter.cn/1932.html/ 一.想要实现的功能 目前已经实现将本地的web服务暴露到公网,现想要实现https访问.(前提:已经有相应的证书文件,如果没有就去申请 ...
- Redis 设计与实现:Redis 对象
本文的分析都是基于 Redis 6.0 版本源码 redis 6.0 源码:https://github.com/redis/redis/tree/6.0 在 Redis 中,有五大数据类型,都统一封 ...