530. 二叉搜索树的最小绝对差【简单】

题目链接


const getList = (root: TreeNode | null, resList: number[]) => {
    if (!root) return;
    if (root.left) getList(root.left, resList);
    resList.push(root.val);
    if (root.right) getList(root.right, resList);
    return;
}

function getMinimumDifference(root: TreeNode | null): number {
    const valList = [] as number[];
    getList(root, valList);
    let minRes = Number.MAX_VALUE;
    valList.forEach((val, id) => {
        if (id+1<valList.length) {
            minRes = Math.min(minRes, valList[id+1] - val);
        }
    });
    return minRes
};

results matching ""

    No results matching ""