当前位置:网站首页>Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
2022-07-07 08:04:00 【Hydrion-Qlz】
530. The minimum absolute difference of binary search tree
The difficulty is simple
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 .
Ideas
The problem needs to find the minimum of the absolute value of the difference between any two nodes on the binary search tree .
Notice the binary search tree , Binary search trees are ordered !
Traverse the binary search tree , What you get is actually an ordered array , Find the minimum difference on an ordered array , Is it very simple ?
package cn.edu.xjtu.carlWay.tree.minAbsoluteDifferenceInBST;
import cn.edu.xjtu.Util.TreeNode.TreeNode;
/** * 530. The minimum absolute difference of binary search tree * Give you a binary search tree root node root , return The minimum difference between the values of any two different nodes in the tree . * <p> * The difference is a positive number , Its value is equal to the absolute value of the difference between the two values . * <p> * https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/ */
public class Solution {
int result = Integer.MAX_VALUE;
TreeNode pre = null;
/** * If you think of it as a mid order traversal, it is an ordered array * * @param root * @return */
public int getMinimumDifference(TreeNode root) {
if (root == null) return 0;
traversal(root);
return result;
}
private void traversal(TreeNode root) {
if (root == null) {
return;
}
// Traverse left subtree
traversal(root.left);
// Data processing in the middle
if (pre != null) {
result = Math.min(result, root.val - pre.val);
}
pre = root;
// Traversal right subtree
traversal(root.right);
}
}
边栏推荐
- C语言航班订票系统
- 【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
- Linux server development, MySQL transaction principle analysis
- 2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
- 2022焊工(初级)判断题及在线模拟考试
- pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
- [UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
- Visualization Document Feb 12 16:42
- Info | webrtc M97 update
- Leetcode 43 String multiplication (2022.02.12)
猜你喜欢

QT learning 28 toolbar in the main window

Few shot Learning & meta learning: small sample learning principle and Siamese network structure (I)

Qt学习28 主窗口中的工具栏
![[UTCTF2020]file header](/img/e3/818e2d531a06ab90de189055f634ad.png)
[UTCTF2020]file header

Explore Cassandra's decentralized distributed architecture
![[unity] several ideas about circular motion of objects](/img/84/e70c6696629dbe17ace011553f43ff.png)
[unity] several ideas about circular motion of objects

Custom class loader loads network class

The charm of SQL optimization! From 30248s to 0.001s

【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)

Leetcode 40: combined sum II
随机推荐
[2022 ciscn] replay of preliminary web topics
贝叶斯定律
padavan手动安装php
Explore dry goods! Apifox construction ideas
Zhilian + AV, AITO asked M7 to do more than ideal one
Codeforces Global Round 19
Record a stroke skin bone error of the skirt
QT learning 26 integrated example of layout management
C语言队列
The principle and implementation of buffer playback of large video files
有 Docker 谁还在自己本地安装 Mysql ?
运放电路的反馈电阻上并联一个电容是什么作用
JS quick start (I)
微信小程序基本组件使用介绍
C语言航班订票系统
探索Cassandra的去中心化分布式架构
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
[unity] several ideas about circular motion of objects
[experience sharing] how to expand the cloud service icon for Visio
Ansible