当前位置:网站首页>Leetcode skimming: binary tree 22 (minimum absolute difference of binary search tree)
Leetcode skimming: binary tree 22 (minimum absolute difference of binary search tree)
2022-07-07 12:48:00 【Taotao can't learn English】
530. The minimum absolute difference of binary search tree
Give you a binary search tree with nonnegative nodes , Please calculate the minimum absolute value of the difference between any two nodes in the tree .
Example :
Tips : At least in the tree 2 Nodes .
Binary search tree , Itself in ascending order , The decreasing , Intermediate order traversal to obtain value , Compare one by one
public int getMinimumDifference(TreeNode root) {
List<Integer> result = new ArrayList();
// Traverse the binary tree to get the result set
// Binary search tree itself is ascending
inorderTraversal(root, result);
// Define an impossible maximum
int minimumDifference = 100001;
for (int i = 1; i < result.size(); i++) {
if (minimumDifference == 0) {
return minimumDifference;
}
if (result.get(i) - result.get(i - 1) < minimumDifference) {
minimumDifference = result.get(i) - result.get(i - 1);
}
}
return minimumDifference;
}
public void inorderTraversal(TreeNode root, List<Integer> result) {
if (root == null) {
return;
}
// In the sequence traversal Left in Right
inorderTraversal(root.left, result);
result.add(root.val);
inorderTraversal(root.right, result);
}
Looking at the solution, I found that my writing was complicated , The solution of the problem is written with the parent pointer , Comparison in traversal .
TreeNode parent;
int result = Integer.MAX_VALUE;
public void inorderTraversalParent(TreeNode root) {
if (root == null) {
return;
}
// In the sequence traversal Left in Right
inorderTraversalParent(root.left);
if (parent != null) {
if (result > root.val - parent.val) {
result = root.val - parent.val
}
}
parent = root;
inorderTraversalParent(root.right);
}
public int getMinimumDifference2(TreeNode root) {
inorderTraversalParent(root);
return result;
}
边栏推荐
- leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
- RHSA first day operation
- Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
- SQL Lab (46~53) (continuous update later) order by injection
- Object. Simple implementation of assign()
- Routing strategy of multi-point republication [Huawei]
- File upload vulnerability - upload labs (1~2)
- leetcode刷题:二叉树26(二叉搜索树中的插入操作)
- Importance of database security
- GCC compilation error
猜你喜欢
JS to convert array to tree data
Experiment with a web server that configures its own content
The IDM server response shows that you do not have permission to download the solution tutorial
Master公式。(用于计算递归的时间复杂度。)
BGP actual network configuration
2022危险化学品生产单位安全生产管理人员考题及在线模拟考试
BGP third experiment report
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
Attack and defense world ----- summary of web knowledge points
[爬虫]使用selenium时,躲避脚本检测
随机推荐
【从 0 开始学微服务】【02】从单体应用走向服务化
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
有什么类方法或是函数可以查看某个项目的Laravel版本的?
layer弹出层的关闭问题
[statistical learning method] learning notes - logistic regression and maximum entropy model
OSPF exercise Report
HZOJ #236. 递归实现组合型枚举
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
HZOJ #240. 图形打印四
Zhimei creative website exercise
RHSA first day operation
Four functions of opencv
[learn microservice from 0] [01] what is microservice
SQL Lab (41~45) (continuous update later)
[learn microservices from 0] [03] explore the microservice architecture
Importance of database security
【统计学习方法】学习笔记——支持向量机(上)
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
Common knowledge of one-dimensional array and two-dimensional array