当前位置:网站首页>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;
}
边栏推荐
- The left-hand side of an assignment expression may not be an optional property access.ts(2779)
- ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
- 【从 0 开始学微服务】【01】什么是微服务
- SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
- [statistical learning method] learning notes - logistic regression and maximum entropy model
- 对话PPIO联合创始人王闻宇:整合边缘算力资源,开拓更多音视频服务场景
- 【PyTorch实战】图像描述——让神经网络看图讲故事
- 利用栈来实现二进制转化为十进制
- visual stdio 2017关于opencv4.1的环境配置
- [learn wechat from 0] [00] Course Overview
猜你喜欢
随机推荐
Static comprehensive experiment
GCC compilation error
【PyTorch实战】用RNN写诗
leetcode刷题:二叉树23(二叉搜索树中的众数)
【从 0 开始学微服务】【01】什么是微服务
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
静态Vxlan 配置
Configure an encrypted web server
@Resource和@Autowired的区别?
NPM instal reports agent or network problems
leetcode刷题:二叉树24(二叉树的最近公共祖先)
layer弹出层的关闭问题
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
leetcode刷题:二叉树19(合并二叉树)
[pytorch practice] image description -- let neural network read pictures and tell stories
[learn microservices from 0] [03] explore the microservice architecture
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
Minimalist movie website
[statistical learning method] learning notes - support vector machine (Part 2)