问题:
https://leetcode.com/problems/minimum-absolute-difference-in-bst/?tab=Description
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. 给定一个二叉搜索树,返回这棵树中任意两个节点差值绝对值最小的那个。
Example:
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).
分析:
中序遍历BST得到的节点值是递增的,因此只需要比较中序遍历时所有相邻节点的绝对差即可得到最小绝对差。第94题和第145题都是中序遍历的问题。
参考C++代码:
LeetCode-530. Minimum Absolute Difference in BST
最新推荐文章于 2024-07-15 12:10:21 发布