【问题标题】:Checking if a Binary Search Tree is Valid javascript检查二叉搜索树是否有效 javascript
【发布时间】:2016-03-06 19:43:28
【问题描述】:

我在网上遇到了这个问题,我发现了以下函数来检查 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 时)。不知何故 min 从 null 变为 1 我不知道如何,我想你需要某种基本情况才能将最小值从 null 变为其他东西...... 当我在每次运行时 console.log 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

【问题讨论】:

  • 您正在使用此表达式if (!isValidBST(node.left, min, node.val) || !isValidBST(node.right, node.val, max)) { 显式调用具有非空值的 isValidBST
  • 对,但是怎么做呢? min如何变成非空值?
  • 因为你传入了node.val isValidBST(node.right, node.val, max) 所以node.val 不能为空。
  • 你想知道的一切都在这里:stackoverflow.com/questions/499995/…
  • @bhspencer 哦,我想我现在明白了。这两个单独的函数调用知道彼此的变量(最小值,最大值),我认为这就是让我绊倒的原因。随时发布作为答案,我会接受!

标签: javascript algorithm data-structures binary-search-tree


【解决方案1】:

变量min 变为非空,因为您显式调用了

isValidBST(node.right, node.val, max)

您将 node.val 作为参数 min 传递的位置。一定是在您进行此调用时node.val 不为空;

【讨论】:

    【解决方案2】:

    给定一个节点,验证二叉搜索树, 确保每个节点的左手孩子都是 小于父节点的值,并且 每个节点的右手子节点都大于 父母

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

    【讨论】:

      【解决方案3】:

      另一种解决方案可能是:

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 2018-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-05-16
          • 2019-04-09
          • 2020-04-07
          • 1970-01-01
          • 2012-06-05
          相关资源
          最近更新 更多