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 ...
随机推荐
- mysql 5.7.26 忘记root密码
1.关闭mysql [root@mysql ~]# /etc/init.d/mysqld stopShutting down MySQL.. SUCCESS! 2.修改参数文件/etc/my.cnf ...
- PDCA
Plan(规划) Do(执行) Check(验证) Adjust(调整)
- 蒲公英 · JELLY技术周刊 Vol.34: 芜湖~ Flutter
蒲公英 · JELLY技术周刊 Vol.34 提及跨端,你能想到那些技术?PWA.小程序.Ionic.React Native.Weex--当然也少不了 Flutter,历时 3 年,Flutter ...
- 网络 IO 模型简单介绍
一.同步阻塞 IO(BIO) 当用户线程调用了 read 系统调用,内核(kernel)就开始了 IO 的第一个阶段:准备数据.很多时候,数据在一开始还没有到达(比如,还没有收到一个完整的Socket ...
- wpa_supplicant 检测错误密码
选好了 wifi ssid,填了密码,生成新配置文件,重启了wpa_supplicant,怎么知道输入的密码对不对,如果不对有什么体现? wpa_supplicant 前台运行时,打印信息中会有: W ...
- DRF使用超链接API实现真正RESTful
很多API并不是真正的实现了RESTful,而应该叫做RPC (Remote Procedure Call 远程过程调用),Roy Fielding曾经提到了它们的区别,原文如下: I am gett ...
- 使用SimpleDateFormat验证日期格式
Java中日期格式的验证有很多方式,这里介绍用 java.text.SimpleDateFormat 来实现时间验证的一种简单方式.首先我们要知道 SimpleDateFormat 对象有一个方法 v ...
- Windows系统提示:“windows找不到文件请确定文件名是否正确后
最近使用Win7/10系统的用户反应在系统中移动了桌面上的一些与系统无关的文档,在挪动了文件之后出现的问题,弹出了windows找不到文件请确定文件名是否正确后,再试一次, 的错误提示,该怎么办呢? ...
- 在Ubuntu14.04下配置Samba 完成linux和windows之间的文件共享
在Windows和Linux之间传递文件可以使用Samba服务.下面是安装步骤: 1. 安装Samba. sudo apt-get install samba 2. 修改配置文件 sudo gedit ...
- 伯俊BOS2.0店铺收入对账功能设计
一.客户需求 通过导入银行POS机流水,将流水与ERP系统的零售付款数据进行对比,统计差异! 二.功能设计 1.新增"POS机号对应表单",用于维护POS机与erp店仓对应 2.新 ...