当前位置:网站首页>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;
}
边栏推荐
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- IPv6 experiment
- Vxlan static centralized gateway
- Hi3516 full system type burning tutorial
- Is it safe to open Huatai's account in kainiu in 2022?
- (待会删)yyds,付费搞来的学术资源,请低调使用!
- Will the filing free server affect the ranking and weight of the website?
- SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
- Sonar:cognitive complexity
- 利用栈来实现二进制转化为十进制
猜你喜欢
Attack and defense world ----- summary of web knowledge points
30. Feed shot named entity recognition with self describing networks reading notes
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
静态Vxlan 配置
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~
Vxlan 静态集中网关
【统计学习方法】学习笔记——支持向量机(下)
Vxlan static centralized gateway
浅谈估值模型 (二): PE指标II——PE Band
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
随机推荐
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
AirServer自动接收多画面投屏或者跨设备投屏
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Several methods of checking JS to judge empty objects
Introduction to three methods of anti red domain name generation
EPP+DIS学习之路(2)——Blink!闪烁!
[deep learning] image multi label classification task, Baidu paddleclas
牛客网刷题网址
IPv6 experiment
Vxlan 静态集中网关
SQL Lab (41~45) (continuous update later)
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
Visual studio 2019 (localdb) \mssqllocaldb SQL Server 2014 database version is 852 and cannot be opened. This server supports version 782 and earlier
Preorder, inorder and postorder traversal of binary tree
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
What is a LAN domain name? How to parse?
防红域名生成的3种方法介绍
SQL lab 1~10 summary (subsequent continuous update)
TypeScript 接口继承
《看完就懂系列》天哪!搞懂节流与防抖竟简单如斯~