当前位置:网站首页>leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
2022-07-07 10:30:00 【涛涛英语学不进去】
530.二叉搜索树的最小绝对差
给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。
示例:

提示:树中至少有 2 个节点。
二叉搜索树,本身升序,非递减,中序遍历获取值,一个个比较
public int getMinimumDifference(TreeNode root) {
List<Integer> result = new ArrayList();
//遍历二叉树获取结果集
//二叉搜索树本身就是升序的
inorderTraversal(root, result);
//定义一个不可能出现的最大值
int minimumDifference = 100001;
for (int i = 1; i < result.size(); i++) {
if (minimumDifference == 0) {
return minimumDifference;
}
if (result.get(i) - result.get(i - 1) < minimumDifference) {
minimumDifference = result.get(i) - result.get(i - 1);
}
}
return minimumDifference;
}
public void inorderTraversal(TreeNode root, List<Integer> result) {
if (root == null) {
return;
}
//中序遍历 左 中 右
inorderTraversal(root.left, result);
result.add(root.val);
inorderTraversal(root.right, result);
}

看看题解发现我写复杂了,题解的写法是用父指针,遍历中比较。
TreeNode parent;
int result = Integer.MAX_VALUE;
public void inorderTraversalParent(TreeNode root) {
if (root == null) {
return;
}
//中序遍历 左 中 右
inorderTraversalParent(root.left);
if (parent != null) {
if (result > root.val - parent.val) {
result = root.val - parent.val
}
}
parent = root;
inorderTraversalParent(root.right);
}
public int getMinimumDifference2(TreeNode root) {
inorderTraversalParent(root);
return result;
}

边栏推荐
- BGP actual network configuration
- An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
- OSPF exercise Report
- EPP+DIS学习之路(1)——Hello world!
- SQL blind injection (WEB penetration)
- sql-lab (54-65)
- 免备案服务器会影响网站排名和权重吗?
- 【PyTorch实战】图像描述——让神经网络看图讲故事
- Cenos openssh upgrade to version 8.4
- Xiaohongshu microservice framework and governance and other cloud native business architecture evolution cases
猜你喜欢

<No. 9> 1805. Number of different integers in the string (simple)

The IDM server response shows that you do not have permission to download the solution tutorial

Hi3516 full system type burning tutorial

Problem: the string and characters are typed successively, and the results conflict

The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful

Processing strategy of message queue message loss and repeated message sending

ES底层原理之倒排索引

Introduction and application of smoothstep in unity: optimization of dissolution effect

数据库系统原理与应用教程(007)—— 数据库相关概念

wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
随机推荐
(to be deleted later) yyds, paid academic resources, please keep a low profile!
Is it safe to open Huatai's account in kainiu in 2022?
Solutions to cross domain problems
File upload vulnerability - upload labs (1~2)
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
Completion report of communication software development and Application
Utiliser la pile pour convertir le binaire en décimal
"Series after reading" my God! It's so simple to understand throttling and anti shake~
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
Sonar:cognitive complexity
What is an esp/msr partition and how to create an esp/msr partition
【统计学习方法】学习笔记——支持向量机(上)
Hi3516全系统类型烧录教程
<No. 8> 1816. 截断句子 (简单)
Configure an encrypted web server
zero-shot, one-shot和few-shot
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
The hoisting of the upper cylinder of the steel containment of the world's first reactor "linglong-1" reactor building was successful
免备案服务器会影响网站排名和权重吗?