LeetCode 530. Minimum Absolute Difference in BST
题目描述:
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 Input:
1
\
3
/
2
Output:
1
Explanation:
The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).Note: There are at least two nodes in this BST.
一个只包含非负元素的二叉搜索树中找到两个元素之间的最小差的绝对值。因为是二叉搜索树所以用中序遍历就可以得到一个有序序列,最小的差值一定是出现在相邻的元素中,只比较相邻元素的差值即可。
1 | /** |