当前位置:网站首页>Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
2022-07-07 08:04:00 【Hydrion-Qlz】
530. The minimum absolute difference of binary search tree
The difficulty is simple
Give you a binary search tree root node root , return The minimum difference between the values of any two different nodes in the tree .
The difference is a positive number , Its value is equal to the absolute value of the difference between the two values .
Ideas
The problem needs to find the minimum of the absolute value of the difference between any two nodes on the binary search tree .
Notice the binary search tree , Binary search trees are ordered !
Traverse the binary search tree , What you get is actually an ordered array , Find the minimum difference on an ordered array , Is it very simple ?
package cn.edu.xjtu.carlWay.tree.minAbsoluteDifferenceInBST;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 530. The minimum absolute difference of binary search tree * Give you a binary search tree root node root , return The minimum difference between the values of any two different nodes in the tree . * <p> * The difference is a positive number , Its value is equal to the absolute value of the difference between the two values . * <p> * https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/ */
public class Solution {
int result = Integer.MAX_VALUE;
TreeNode pre = null;
/** * If you think of it as a mid order traversal, it is an ordered array * * @param root * @return */
public int getMinimumDifference(TreeNode root) {
if (root == null) return 0;
traversal(root);
return result;
}
private void traversal(TreeNode root) {
if (root == null) {
return;
}
// Traverse left subtree
traversal(root.left);
// Data processing in the middle
if (pre != null) {
result = Math.min(result, root.val - pre.val);
}
pre = root;
// Traversal right subtree
traversal(root.right);
}
}
边栏推荐
- 3D reconstruction - stereo correction
- [Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
- Yugu p1020 missile interception (binary search)
- Explore Cassandra's decentralized distributed architecture
- Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
- Cnopendata list data of Chinese colleges and Universities
- Numbers that appear only once
- [mathematical notes] radian
- php导出百万数据
- Linux server development, redis source code storage principle and data model
猜你喜欢

Record a stroke skin bone error of the skirt

Leetcode 90: subset II
![[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead](/img/e3/cceede6babae3c8a24336c81d98aa7.jpg)
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead

Thinkcmf6.0安装教程

Cnopendata list data of Chinese colleges and Universities

LeetCode 40:组合总和 II

Codeforce c.strange test and acwing

【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)

Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)
![[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)](/img/e1/9a047ef13299b94b5314ee6865ba26.png)
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
随机推荐
Zhilian + AV, AITO asked M7 to do more than ideal one
Button wizard script learning - about tmall grabbing red envelopes
Figure out the working principle of gpt3
Sign up now | oar hacker marathon phase III, waiting for your challenge
Regular e-commerce problems part1
Linux server development, MySQL transaction principle analysis
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
青龙面板--花花阅读
大视频文件的缓冲播放原理以及实现
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
Linux server development, MySQL process control statement
【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
JS quick start (I)
Use and analysis of dot function in numpy
Quickly use Jacobo code coverage statistics
运放电路的反馈电阻上并联一个电容是什么作用
pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!
C语言队列