验证是否为二叉搜索树

甄建雨要努力学习啊 / 2024-03-07 / 原文

const isValidBinarySearchTree = (tree) => {
  const stack = [Number.MIN_VALUE];
  const loop = (node) => {
    if (node === null) return;
    loop(node.left);
    if (stack[stack.length - 1] >= node.value) return false;
    stack.push(node.value);
    loop(node.right);
    return true;
  };
  return loop(tree);
};