当前位置:网站首页>Binary tree - 530. Minimum absolute difference of binary search tree
Binary tree - 530. Minimum absolute difference of binary search tree
2022-07-26 00:04:00 【Xiao Zhao, who is working hard for millions of annual salary】
1 Title Description
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 .
Inherent 783. Binary search tree node minimum distance It's actually the same question
2 Title Example
Example 1:
Input :root = [4,2,6,1,3]
Output :1
Example 2:
Input :root = [1,0,48,null,null,12,49]
Output :1
source : Power button (LeetCode)
link :https://leetcode.cn/problems/minimum-absolute-difference-in-bst
3 Topic tips
The number of nodes in the tree ranges from [2, 10^4]
0 <= Node.val <= 10^5
4 Ideas
Consider for ascending arrays aa Find the minimum of the absolute value of the difference between any two elements , The answer must be the minimum of the difference between two adjacent elements , namely 
among n Is an array α The length of . Any other interval greater than or equal to 2 Subscript pair of (i,j) The difference between the elements of — Greater than subscript pair (i,i+1) The difference between the elements of , So no
To be considered again .
Back to the subject , This problem requires the minimum absolute value of the difference between any two nodes of the binary search tree , We know that the binary search tree has a property that the sequence of values obtained by order traversal in the binary search tree is incrementally ordered , Therefore, as long as we get the value sequence after middle order traversal, we can use the method mentioned above to solve .
The simple way is through — Secondary middle order traversal saves the value in an array and then performs Traversal Solution , We can also use... In the middle order traversal process pre Variable holds the value of the precursor node , In this way, the answer can be updated while traversing , There is no need to explicitly create arrays to hold , It should be noted that pre The initial value of needs to be set to any negative number at the beginning of the tag , The following code is set to -1.
Complexity analysis
Time complexity :O(n), among n The number of nodes in the binary search tree . Each node will be visited once and only once in the middle order traversal , So the total time complexity is O(n).
Spatial complexity :O(n). The space complexity of recursive functions depends on the stack depth of recursion , The stack depth in the binary search tree is — In the case of a chain, it will reach O(n) Level .
5 My answer
class Solution {
int pre;
int ans;
public int getMinimumDifference(TreeNode root) {
ans = Integer.MAX_VALUE;
pre = -1;
dfs(root);
return ans;
}
public void dfs(TreeNode root) {
if (root == null) {
return;
}
dfs(root.left);
if (pre == -1) {
pre = root.val;
} else {
ans = Math.min(ans, root.val - pre);
pre = root.val;
}
dfs(root.right);
}
}
边栏推荐
- Lua script Wireshark plug-in to resolve third-party private protocols
- [ManageEngine] servicedesk plus won the 2022 safety model engineering data safety award
- [learning notes] solid works operation record
- TOPSIS and entropy weight method
- 行为型模式之观察者模式
- NVIDIA可编程推理加速器TensorRT学习笔记(三)——加速推理
- Nacos offline service times error errcode: 500
- Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
- Yolov3 trains its own data set
- Stack and queue - 150. Inverse Polish expression evaluation
猜你喜欢
随机推荐
Yolov3 trains its own data set
指针函数的demo
The difference between SFTP and FTP
二叉树——101. 对称二叉树
1223. Dice simulation range DP
Typescript TS basic knowledge and so on
行为型模式之迭代器模式
【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
Responsibility chain model of behavioral model
Song of statistics lyrics
Dead letter queue and message TTL expiration code
二叉树——654. 最大二叉树
二叉树——104. 二叉树的最大深度
栈与队列——347. 前 K 个高频元素
MySQL的DDL、DML和DQL的基本语法
回溯——77. 组合
浅识 OWASP
回溯——17. 电话号码的字母组合
Sort fake contacts
模块二作业








![[learning notes] unreal 4 engine introduction (IV)](/img/30/4defa3cbd785d43adb405c71d16406.png)
