当前位置:网站首页>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);
}
边栏推荐
- Some methods of string
- NVIDIA Jetson Nano/Xavier NX 扩容教程
- Service architecture diagram of Nacos series
- Leetcode- intersection of two arrays ii- simple
- 13 cancelendevent of a flowable end event and compensationthrowing of a compensation event
- Sentinel series hot spot current limiting
- Leetcode- longest palindrome string - simple
- Vagrant virtual machine installation, disk expansion and LAN access tutorial
- Unity game optimization (version 2) learning record 7
- Django uploads local binaries to the database filefield field
猜你喜欢
OpenGL马赛克(八)
17 servicetask of flowable task
2021.9.30 learning log -postman
Service architecture diagram of Nacos series
[China & some provinces and cities] JSON file for offline map visualization
Feel the power of shardingsphere JDBC through the demo
Building a stand-alone version of Nacos series
软件测试——接口常见问题汇总
Web site learning and sorting
Use the browser to cut the entire page (take chrome as an example)
随机推荐
Sentinel series integrates Nacos and realizes dynamic flow control
Some methods of string
Django uses redis to store sessions starting from 0
Summary of the 11th week of sophomore year
JS output uincode code
Integer tips
Building a stand-alone version of Nacos series
Missing tag identification in cots RFID systems: bringing the gap between theory and Practice
Solution to prompt "permission is required to perform this operation" (file cannot be deleted) when win10 deletes a file
arrayList && linkedList
Timeout thread log for tongweb
Find out the missing numbers from the natural numbers arranged in order from 0 to 100, and the solution provides
Pychart professional edition's solution to SQL script error reporting
@Detailed explanation of propertysource usage method and operation principle mechanism
Leetcode- first unique character in string - simple
Service architecture diagram of Nacos series
What happens when the MySQL union index ABC encounters a "comparison operator"?
Concurrent programming -- countdownlatch combined with thread pool
How to Algorithm Evaluation Methods
No assessment summary