Minimum Absolute Difference in BST

求非负二叉搜索树中任意两个节点值的差的绝对值的最小值。

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;
    }

猜你喜欢

转载自blog.csdn.net/Wenbooboo/article/details/80774792