求非负二叉搜索树中任意两个节点值的差的绝对值的最小值。
There are at least two nodes in this BST.
利用搜索二叉树中序遍历的有序性来求解
private int min = int.MaxValue;
int? temp = null;
int GetMinimumDifference(TreeNode head)
{
if (head == null)
return min;
GetMinimumDifference(head.leftNode);
if (temp != null)
{
min = Mathf.Min(min, (int)(head.value-temp));
}
temp = head.value;
GetMinimumDifference(head.rightNode);
return min;
}
本文介绍了一种算法,用于求解非空二叉搜索树中任意两节点值差的绝对值最小值。通过中序遍历利用二叉搜索树的特性,实现了高效的查找方法。
380

被折叠的 条评论
为什么被折叠?



