验证是否为二叉搜索树

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

  

posted @ 2024-03-07 20:52  671_MrSix  阅读(3)  评论(0编辑  收藏  举报