当前位置:网站首页>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);
}
}
边栏推荐
- LeetCode 190. 颠倒二进制位 位运算/easy
- Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)
- STM32 can communication filter setting problem
- 网络设备硬核技术内幕 路由器篇 9 CISCO ASR9900拆解 (二)
- Discussion on STM32 power down reset PDR
- Leetcode 244周赛-赛后补题题解【西兰花选手】
- The design method of integral operation circuit is introduced in detail
- "Sword finger offer" linked list inversion
- 网络设备硬核技术内幕 路由器篇 16 DPDK及其前传(一)
- How "Crazy" is Hefu Laomian, which is eager to be listed, with capital increasing frequently?
猜你喜欢

Unity mouse controls the first person camera perspective

反射

Kubernetes CNI classification / operation mechanism
仪表放大器和运算放大器优缺点对比

JMeter recording interface automation

reflex

ADB command (install APK package format: ADB install APK address package name on the computer)

Basic usage of kotlin

AssetBundle如何打包

谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues
随机推荐
Method of removing top navigation bar in Huawei Hongmeng simulator
Four kinds of relay schemes driven by single chip microcomputer
Unity's simplest object pool implementation
网络设备硬核技术内幕 路由器篇 15 从鹿由器到路由器 (下)
Code coverage statistical artifact -jacobo tool practice
Unity performance optimization ----- drawcall
华云数据打造完善的信创人才培养体系 助力信创产业高质量发展
Leetcode-1737- minimum number of characters to change if one of the three conditions is met
Leetcode 244 week competition - post competition supplementary question solution [broccoli players]
谷粒商城配置CorsWebFilter后,报错:Resource sharing error:MultipleAllowOriginValues
Network equipment hard core technology insider router Chapter 9 Cisco asr9900 disassembly (II)
STM32之CAN ---CAN ID过滤器分析
网络设备硬核技术内幕 路由器篇 3 贾宝玉梦游太虚幻境 (中)
Discussion on STM32 power down reset PDR
Stm32f103c8t6 drives ssd1306 0.96 "IIC OLED display under Arduino frame
Notice of Nanshan District Civil Affairs Bureau on carrying out the grade evaluation of social organizations in Nanshan District in 2022
Network equipment hard core technology insider router Chapter 3 Jia Baoyu sleepwalking in Taixu Fantasy (middle)
What is tor? What is the use of tor browser update?
Selenium 报错:session not created: This version of ChromeDriver only supports Chrome version 81
基于stm32的数字示波器设计方案