当前位置:网站首页>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);
}
}
边栏推荐
- Leanote private cloud note building
- Linux server development, MySQL stored procedures, functions and triggers
- C语言航班订票系统
- Binary tree and heap building in C language
- 2022年全国最新消防设施操作员(初级消防设施操作员)模拟题及答案
- 太真实了,原来自己一直没有富裕起来是有原因的
- 2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
- Linux server development, redis source code storage principle and data model
- LeetCode 40:组合总和 II
- [quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
猜你喜欢
Common validation comments
LeetCode 90:子集 II
探索干货篇!Apifox 建设思路
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
Most elements
Main window in QT learning 27 application
The charm of SQL optimization! From 30248s to 0.001s
[UTCTF2020]file header
Numbers that appear only once
Linux server development, MySQL transaction principle analysis
随机推荐
[UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
buuctf misc USB
Linux server development, redis source code storage principle and data model
Niu Mei's mathematical problem --- combinatorial number
2022年茶艺师(中级)考试试题及模拟考试
Lattice coloring - matrix fast power optimized shape pressure DP
青龙面板--花花阅读
The charm of SQL optimization! From 30248s to 0.001s
Force buckle 145 Binary Tree Postorder Traversal
QT learning 26 integrated example of layout management
Zsh shell adds automatic completion and syntax highlighting
JS quick start (I)
Linux server development, detailed explanation of redis related commands and their principles
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
C语言航班订票系统
Content of string
mysql多列索引(组合索引)特点和使用场景
Thinkcmf6.0安装教程
Explore Cassandra's decentralized distributed architecture
Cnopendata American Golden Globe Award winning data