当前位置:网站首页>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);
}
}
边栏推荐
- Téléchargement des données de conception des puces
- Rust Versus Go(哪种是我的首选语言?)
- Visualization Document Feb 12 16:42
- [mathematical notes] radian
- Record a stroke skin bone error of the skirt
- Leetcode 40: combined sum II
- 芯片 设计资料下载
- Use and analysis of dot function in numpy
- [quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
- Linux server development, MySQL stored procedures, functions and triggers
猜你喜欢
Common validation comments
2022年茶艺师(中级)考试试题及模拟考试
[Matlab] Simulink 自定义函数中的矩阵乘法工作不正常时可以使用模块库中的矩阵乘法模块代替
LeetCode 40:组合总和 II
mysql多列索引(组合索引)特点和使用场景
Sign up now | oar hacker marathon phase III, waiting for your challenge
SQL优化的魅力!从 30248s 到 0.001s
Introduction to basic components of wechat applet
LeetCode中等题之我的日程安排表 I
Quickly use Jacobo code coverage statistics
随机推荐
Force buckle 144 Preorder traversal of binary tree
You Li takes you to talk about C language 6 (common keywords)
Lattice coloring - matrix fast power optimized shape pressure DP
Value sequence (subsequence contribution problem)
JSON data flattening pd json_ normalize
Who has docker to install MySQL locally?
央视太暖心了,手把手教你写HR最喜欢的简历
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
LeetCode 90:子集 II
Explore dry goods! Apifox construction ideas
PHP exports millions of data
Common method signatures and meanings of Iterable, collection and list
Rust versus go (which is my preferred language?)
Linux server development, MySQL cache strategy
Shell 脚本的替换功能实现
自定义类加载器加载网络Class
game攻防世界逆向
Zhilian + AV, AITO asked M7 to do more than ideal one
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers