当前位置:网站首页>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;
}

边栏推荐
- Using stack to convert binary to decimal
- 金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
- In the small skin panel, use CMD to enter the MySQL command, including the MySQL error unknown variable 'secure_ file_ Priv 'solution (super detailed)
- 【PyTorch实战】用RNN写诗
- College entrance examination composition, high-frequency mention of science and Technology
- 解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
- 密码学系列之:在线证书状态协议OCSP详解
- <No. 9> 1805. 字符串中不同整数的数目 (简单)
- About web content security policy directive some test cases specified through meta elements
- Is it safe to open an account in Ping An Securities mobile bank?
猜你喜欢

Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition

Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed

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

ps链接图层的使用方法和快捷键,ps图层链接怎么做的

OSPF exercise Report

Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具

【PyTorch实战】用RNN写诗

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

Static comprehensive experiment

关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
随机推荐
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
Experiment with a web server that configures its own content
普乐蛙小型5d电影设备|5d电影动感电影体验馆|VR景区影院设备
什么是局域网域名?如何解析?
How much does it cost to develop a small program mall?
TypeScript 接口继承
Configure an encrypted web server
RHSA first day operation
About sqli lab less-15 using or instead of and parsing
Airserver automatically receives multi screen projection or cross device projection
OSPF exercise Report
(to be deleted later) yyds, paid academic resources, please keep a low profile!
关于 Web Content-Security-Policy Directive 通过 meta 元素指定的一些测试用例
Completion report of communication software development and Application
GCC compilation error
Using stack to convert binary to decimal
What is a LAN domain name? How to parse?
[deep learning] image multi label classification task, Baidu paddleclas
Is it safe to open Huatai's account in kainiu in 2022?
How to understand the clothing industry chain and supply chain