检查二叉搜索树是否有效的javascript代码

6

我在线上遇到了这个问题,然后我找到了下面的函数来检查BST是否有效。然而,我不完全理解max/min是如何从null变为可以与之比较的值的。所以在下面的函数中:

//Give the recursive function starting values:

 function checkBST(node) {
  // console.log(node.right);
  return isValidBST(node, null, null);
}


 function isValidBST(node, min, max) {
  console.log(min, max);


  if (node === null) {

    return true;
  }

  if ((max !== null && node.val > max) || (min !== null && node.val < min)) {

    return false;
  }

  if (!isValidBST(node.left, min, node.val) || !isValidBST(node.right, node.val, max)) {

    return false;
  }
  return true;
}



var bst = new BinarySearchTree(8);
bst.insert(3);
bst.insert(1);
bst.insert(6);
bst.insert(10);
bst.insert(4);

当你从左侧最低深度上升时,它会将最低深度的值与其上方的深度进行比较(即当输出 1 3 时)。不知何故,最小值从 null 变为了 1,我没有看到具体原因,我认为你需要一些基本情况才能使最小值从 null 变为其他值......

每次运行时,如果在控制台上输出 min/max,则会得到此结果。

null null
null 8
null 3
null 1
1 3
3 8
3 6
3 4
4 6
6 8
8 null
8 10
10 null

你正在使用以下表达式显式调用具有非空值的isValidBST函数:if (!isValidBST(node.left, min, node.val) || !isValidBST(node.right, node.val, max)) { - bhspencer
对,但是怎么做呢?min 怎么变成非空值的? - devdropper87
1
因为你传入了 isValidBST(node.right, node.val, max),所以 node.val 不能为空。 - bhspencer
1
这里有关于二叉搜索树验证的所有你想知道的内容:https://dev59.com/qnRB5IYBdhLWcg3wz6ed - m69 ''snarky and unwelcoming''
@bhspencer 哦,我现在明白了。这两个单独的函数调用知道彼此的变量(min、max),我想这就是让我困惑的原因。请随意发布为答案,我会接受的! - devdropper87
4个回答

3

对于一个节点,需要验证二叉搜索树是否合法,确保每个节点的左子节点小于父节点的值,并且每个节点的右子节点大于父节点的值。

class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
 }
}

class Tree {
 constructor() {
 this.root = null;
}

isValidBST(node, min = null, max = null) {
if (!node) return true;
if (max !== null && node.data >= max) {
  return false;
}
if (min !== null && node.data <= min) {
  return false;
}
const leftSide = this.isValidBST(node.left, min, node.data);
const rightSide = this.isValidBST(node.right, node.val, max);

return leftSide && rightSide;
}
}

const t = new Node(10);
t.left = new Node(0);
t.left.left = new Node(7);
t.left.right = new Node(4);
t.right = new Node(12);
const t1 = new Tree();
t1.root = t;
console.log(t1.isValidBST(t));

2
变量min变成非空是因为您显式调用了 isValidBST(node.right, node.val, max) 在这里,您将node.val作为参数min传递。这意味着在您进行此调用时,node.val不为空;

1
另一个解决方案可能是:

const isValidBST = (
  root,
  min = Number.MIN_SAFE_INTEGER,
  max = Number.MAX_SAFE_INTEGER
) => {
  if (root == null) return true;
  if (root.val >= max || root.val <= min) return false;
  return (
    isValidBST(root.left, min, root.val) &&
    isValidBST(root.right, root.val, max)
  );
};

0

检查二叉搜索树是否有效:

class BTNode {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

/**
 *
 * @param {BTNode} tree
 * @returns {Boolean}
 */
const isBinarySearchTree = (tree) => {
  if (tree) {
    if (
      tree.left &&
      (tree.left.value > tree.value || !isBinarySearchTree(tree.left))
    ) {
      return false;
    }
    if (
      tree.right &&
      (tree.right.value <= tree.value || !isBinarySearchTree(tree.right))
    ) {
      return false;
    }
  }
  return true;
};

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接