当前位置:网站首页>leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
2022-07-07 10:30:00 【涛涛英语学不进去】
530.二叉搜索树的最小绝对差
给你一棵所有节点为非负值的二叉搜索树,请你计算树中任意两节点的差的绝对值的最小值。
示例:
提示:树中至少有 2 个节点。
二叉搜索树,本身升序,非递减,中序遍历获取值,一个个比较
public int getMinimumDifference(TreeNode root) {
List<Integer> result = new ArrayList();
//遍历二叉树获取结果集
//二叉搜索树本身就是升序的
inorderTraversal(root, result);
//定义一个不可能出现的最大值
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;
}
//中序遍历 左 中 右
inorderTraversal(root.left, result);
result.add(root.val);
inorderTraversal(root.right, result);
}
看看题解发现我写复杂了,题解的写法是用父指针,遍历中比较。
TreeNode parent;
int result = Integer.MAX_VALUE;
public void inorderTraversalParent(TreeNode root) {
if (root == null) {
return;
}
//中序遍历 左 中 右
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;
}
边栏推荐
- Decrypt gd32 MCU product family, how to choose the development board?
- 编译 libssl 报错
- 密码学系列之:在线证书状态协议OCSP详解
- <No. 9> 1805. 字符串中不同整数的数目 (简单)
- BGP third experiment report
- 【PyTorch实战】图像描述——让神经网络看图讲故事
- Sign up now | oar hacker marathon phase III midsummer debut, waiting for you to challenge
- gcc 编译报错
- Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
- Apache installation problem: configure: error: APR not found Please read the documentation
猜你喜欢
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
Experiment with a web server that configures its own content
Static vxlan configuration
解决 Server returns invalid timezone. Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
EPP+DIS学习之路(2)——Blink!闪烁!
What is an esp/msr partition and how to create an esp/msr partition
Sort out the garbage collection of JVM, and don't involve high-quality things such as performance tuning for the time being
Decrypt gd32 MCU product family, how to choose the development board?
Up meta - Web3.0 world innovative meta universe financial agreement
随机推荐
Static routing assignment of network reachable and telent connections
Typescript interface inheritance
Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
What is an esp/msr partition and how to create an esp/msr partition
BGP actual network configuration
【PyTorch实战】图像描述——让神经网络看图讲故事
Idea 2021 Chinese garbled code
Vxlan static centralized gateway
(to be deleted later) yyds, paid academic resources, please keep a low profile!
Customize the web service configuration file
SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
How much does it cost to develop a small program mall?
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
Static vxlan configuration
Zhimei creative website exercise
idm服务器响应显示您没有权限下载解决教程
idea 2021中文乱码
数据库系统原理与应用教程(011)—— 关系数据库
Hi3516 full system type burning tutorial
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6