当前位置:网站首页>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);
}
}
边栏推荐
- Detailed explanation of Kalman filter for motion state estimation
- [VHDL parallel statement execution]
- 芯片资料 网站 易特创芯
- LeetCode 90:子集 II
- 探索干货篇!Apifox 建设思路
- json 数据展平pd.json_normalize
- 芯片 設計資料下載
- Pytest + allure + Jenkins Environment - - achèvement du remplissage de la fosse
- The element with setfieldsvalue set is obtained as undefined with GetFieldValue
- Chip design data download
猜你喜欢

Qt学习27 应用程序中的主窗口

青龙面板-今日头条

mysql多列索引(组合索引)特点和使用场景

JS quick start (I)

2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
![[experience sharing] how to expand the cloud service icon for Visio](/img/42/dba9f78f3fb2049dad8b343b0b36e5.png)
[experience sharing] how to expand the cloud service icon for Visio

【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门

Hands on deep learning (IV) -- convolutional neural network CNN

LeetCode 40:组合总和 II

【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
随机推荐
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
Chip information website Yite Chuangxin
SQL优化的魅力!从 30248s 到 0.001s
大视频文件的缓冲播放原理以及实现
2022 tea master (intermediate) examination questions and mock examination
Linux server development, MySQL cache strategy
json 数据展平pd.json_normalize
Padavan manually installs PHP
pytest+allure+jenkins環境--填坑完畢
Most elements
Force buckle 145 Binary Tree Postorder Traversal
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
[2022 actf] Web Topic recurrence
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
Qt学习26 布局管理综合实例
Linux server development, redis source code storage principle and data model
The charm of SQL optimization! From 30248s to 0.001s
LeetCode 90:子集 II
Binary tree and heap building in C language
QT learning 28 toolbar in the main window