当前位置:网站首页>Leetcode minimum absolute difference of binary search tree simple
Leetcode minimum absolute difference of binary search tree simple
2022-06-13 05:49:00 【AnWenRen】
title :530 The minimum absolute difference of binary search tree - Simple
subject
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 .
Example 1
Input :root = [4,2,6,1,3]
Output :1
Example 2
Input :root = [1,0,48,null,null,12,49]
Output :1
Tips
- The number of nodes in the tree ranges from
[2, 104]
0 <= Node.val <= 105
Code Java
int pre;
int ans;
public int getMinimumDifference(TreeNode root) {
pre = -1;
ans = Integer.MAX_VALUE;
inOrder(root);
return ans;
}
// Middle order traversal to obtain the minimum value
public void inOrder(TreeNode root) {
if (root == null) return;
inOrder(root.left);
if (pre == -1)
pre = root.val;
else {
ans = Math.min(ans, root.val - pre);
pre = root.val;
}
inOrder(root.right);
}
边栏推荐
- Power of leetcode-4 - simple
- 为什么那么多人讨厌A-Spice
- The problem of distinguishing and sharing sessions for multiple applications in tongweb
- C calls the API and parses the returned JSON string
- A fast week
- 计算两个时间相差的天数(支持跨月、跨年)
- 2021.9.29 learning log MIME type
- Leetcode- intersection of two arrays ii- simple
- Use the browser to cut the entire page (take chrome as an example)
- 顶部下滑沉浸式dialog
猜你喜欢
Automatic database backup (using Navicat)
AUTOSAR actual combat tutorial pdf version
Agile conflicts and benefits
Solutions to conflicts between xampp and VMware port 443
Sentinel series integrates Nacos and realizes dynamic flow control
12 error end event and terminateendevent of end event
16 the usertask of a flowable task includes task assignment, multi person countersignature, and dynamic forms
1 Introduction to drools rule engine (usage scenarios and advantages)
How slow is the application system on tongweb? How dead is it?
2 first experience of drools
随机推荐
Working principle of sentinel series (concept)
Leetcode- reverse string - simple
Information collection for network security (2)
Getclassloader() returns null, getclassloader() gets null
Shardingsphere JDBC exception: no table route info
ffmpeg 下载后缀为.m3u8的视频文件
Validation set: ‘flowable-executable-process‘ | Problem: ‘flowable-servicetask-missing-implementatio
890. Find and Replace Pattern
Use the browser to cut the entire page (take chrome as an example)
C calls the API and parses the returned JSON string
JNDI configuration for tongweb7
2 first experience of drools
How to set the import / export template to global text format according to the framework = (solve the problem of scientific counting)
Nacos series registry principle and source code analysis
OpenGL馬賽克(八)
How to view tongweb logs correctly?
2021.9.29 learning log MIME type
ArrayList loop removes the pit encountered
Let's talk about how ArrayList is dynamically resized and what kind of mechanism is it?
使用cmake交叉編譯helloworld