当前位置:网站首页>Leetcode 783. binary search tree node minimum distance tree /easy
Leetcode 783. binary search tree node minimum distance tree /easy
2022-07-27 15:27:00 【Abcheche】
List of articles
1.Description
Give you a binary search tree root node root , return The minimum difference between the values of any two different nodes in the tree .
2.Example

Input :root = [4,2,6,1,3]
Output :1
3.Solution
Using middle order traversal , Calculate the difference between the current node and the previous node .
class Solution {
int pre;
int ans;
public int minDiffInBST(TreeNode root) {
ans = Integer.MAX_VALUE;
pre = -1;
dfs(root);
return ans;
}
public void dfs(TreeNode root) {
if (root == null) {
return;
}
dfs(root.left);
if (pre == -1) {
pre = root.val;
} else {
ans = Math.min(ans, root.val - pre);
pre = root.val;
}
dfs(root.right);
}
}
边栏推荐
- 工具 - markdown编辑器常用方法
- Notice of Shenzhen Municipal Bureau of human resources and social security on the issuance of employment related subsidies for people out of poverty
- STM32F10x_ Hardware I2C read / write EEPROM (standard peripheral library version)
- Leetcode 244周赛-赛后补题题解【西兰花选手】
- 网络设备硬核技术内幕 路由器篇 CISCO ASR9900拆解 (一)
- Unity performance optimization ----- drawcall
- "Sword finger offer" linked list inversion
- 仪表放大器和运算放大器优缺点对比
- Reading notes of lifelong growth (I)
- DIY ultra detailed tutorial on making oscilloscope: (1) I'm not trying to make an oscilloscope
猜你喜欢

STM32 can communication filter setting problem
USB接口电磁兼容(EMC)解决方案
Principle of MOS tube to prevent reverse connection of power supply
USB interface electromagnetic compatibility (EMC) solution

Unity mouse controls the first person camera perspective

Leetcode-1737- minimum number of characters to change if one of the three conditions is met

LeetCode 面试题 17.21. 直方图的水量 双指针,单调栈/hard

西瓜书《机器学习》阅读笔记之第一章绪论

generic paradigm

DIY制作示波器的超详细教程:(一)我不是为了做一个示波器
随机推荐
Wechat applet realizes music search page
reflex
Sword finger offer cut rope
魔塔项目中的问题解决
资本频频加码,急于上市的和府捞面有多“疯狂”?
网络设备硬核技术内幕 路由器篇 13 从鹿由器到路由器(上)
网络设备硬核技术内幕 路由器篇 10 CISCO ASR9900拆解 (三)
Network equipment hard core technology insider router chapter Cisco asr9900 disassembly (I)
网络设备硬核技术内幕 路由器篇 6 汤普金森漫游网络世界(中)
STM32 can communication filter setting problem
网络设备硬核技术内幕 路由器篇 21 可重构的路由器
ADB command (install APK package format: ADB install APK address package name on the computer)
Unity's simplest object pool implementation
Unity3D学习笔记10——纹理数组
Unity performance optimization ----- drawcall
generic paradigm
Overview of wechat public platform development
Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81
仅做两项修改,苹果就让StyleGANv2获得了3D生成能力
Data warehouse project is never a technical project