当前位置:网站首页>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);
}
}
边栏推荐
- BOM browser object model
- 抽丝剥茧C语言(高阶)程序环境和预处理
- Prometheus operation and maintenance tool promtool (II) query function
- Leetcode shahutong series -- 63. Different paths II
- J9数字论:什么是DAO模式?DAO发展过程的阻碍
- A long detailed explanation of C language operators
- 程序环境和预处理
- Article 75: writing skills of academic papers
- 注解@Autowired源码解析
- Leetcode107-二叉树的层序遍历II详解
猜你喜欢

Binary tree - 112. Path sum

二叉树——110. 平衡二叉树

调用钉钉api报错:机器人发送签名过期;solution:签名生成时间和发送时间请保持在 timestampms 以内

Kubernetes网络插件详解 - Calico篇 - 概述
34-SparkSQL自定义函数的使用、SparkStreaming的架构及计算流程、DStream转换操作、SparkStreaming对接kafka和offset的处理

Observer model of behavioral model

Call nailing API and report an error: the signature sent by the robot is expired; Solution: please keep the signature generation time and sending time within timestampms

二叉树——104. 二叉树的最大深度

C language actual combat guessing game

TOPSIS and entropy weight method
随机推荐
After entering www.baidu.com in the address bar
【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
J9数字论:什么是DAO模式?DAO发展过程的阻碍
The items of listview will be displayed completely after expansion
如何用yolov5 做个闯红灯监控的智能交通系统(1)
C语言实战之猜拳游戏
Compile live555 with vs2019 in win10
二叉树——404. 左叶子之和
什么是奇偶校验?如何用C语言实现?
栈与队列——239. 滑动窗口最大值
Leetcode200-查找岛屿数量详解
Basic syntax of MySQL DDL, DML and DQL
Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK
The mobile version of Duoyu security browser will add new functions to make users browse more personalized
Stack and queue - 347. Top k high frequency elements
二叉树——111. 二叉树的最小深度
安全文档归档软件
【一库】mapbox-gl!一款开箱即用的地图引擎
Detailed explanation of kubernetes network plug-ins - calico chapter - Overview