【问题标题】:How to store depth of Binary Search Tree如何存储二叉搜索树的深度
【发布时间】:2018-01-20 18:17:14
【问题描述】:

我正在尝试确定二叉搜索树是否平衡。我不太清楚如何存储左右分支的子节点的深度。如果右分支比左分支的长度最大为 1,我试图返回 true,反之亦然。

 /**
     * Definition for a binary tree node.
     * function TreeNode(val) {
     *     this.val = val;
     *     this.left = this.right = null;
     * }
     */
/**
     * @param {TreeNode} root
     * @return {boolean}
     */

var isBalanced = function(root) {
  var checkChild = function(root) { 
    if (this.left) {
      var left = 1;
      this.left.checkChild(root);
      if (this.right) {
        left += 1;
        this.right.checkChild(root);
      }
      if (this.right) {
        var right = 1;
        this.right.checkChild(root);
        if (this.left) {
          right += 1;
          this.right.checkChild(root);
        }
      }
    }
    if (left - right > 1 || right - left > 1) {
      return false;
    }
    return true;
  };
};

我正在考虑创建一个 var 以在每次从头部开始遍历左右分支的节点时递增。但我意识到这会将左分支的节点总数与右分支的节点总数进行比较,这是行不通的。

【问题讨论】:

    标签: javascript binary-search-tree


    【解决方案1】:

    每次检查你为什么要再次发送头像 为什么又root了? this.left.checkChild(root)

    相反,如果您想找到深度,您的实现应该如下所示:

    function treeDepth(tree) 
    {
       if (tree === null) 
           return 0;
       else
       {
           /* compute the depth of each subtree */
           let leftDepth = treeDepth(tree.left);
           let rightDepth = treeDepth(tree.right);
    
           /* use the larger one */
           if (leftDepth > rightDepth) 
               return(leftDepth+1);
           else return(rightDepth+1);
       }
    } 
    

    【讨论】:

      【解决方案2】:

      首先找到根的最大深度,然后找到根的最小深度。使用 dfs 很容易。下一步是检查这些深度的差异。 代码将如下所示:

      class Node {
          constructor(value) {
              this.value = value
              this.left = null
              this.right = null
          }
      }
      
      var isBalanced = function(root) {
          var foo = function(root, fun) {
              if (root === null) return 0
              l = foo(root.left, fun)
              r = foo(root.right, fun)
              return fun(l, r);
          }
          return foo(root, Math.max) - foo(root, Math.min) < 2
      }
      
      let tree = new Node(1)
      tree.left = new Node(2)
      tree.left.left = new Node(3)
      tree.left.left.left = new Node(4)
      tree.right = new Node(5)
      tree.right.left = new Node(6)
      document.write(isBalanced(tree))
      

      【讨论】:

        【解决方案3】:

        如果你想使用checkChild 作为方法,你应该这样定义它,而不是作为一个变量。我还建议不要返回布尔值,而是左子树和右子树之间深度的真正差异。这将为调用者提供更多信息,如果需要,调用者仍然可以将该值视为布尔值(falsy 表示平衡,truthy 表示倾斜)。

        您的实现如下所示:

        class TreeNode {
            constructor(val) {
                this.val = val;
            }
            add(node) {
                const dir = node.val < this.val ? "left" : "right";
                if (this[dir]) {
                    this[dir].add(node);
                } else {
                    this[dir] = node;
                }
            }
            height() {
                return Math.max(
                    this.left ? this.left.height() + 1 : 0,
                    this.right ? this.right.height() + 1 : 0
                );
            }
            tilt() {
                return (this.left  ? this.left.height() + 1 : 0)
                     - (this.right ? this.right.height() + 1 : 0);
            }
            static from(...data) {
                if (!data.length) return;
                const root = new TreeNode(data[0]);
                for (let v of data.slice(1)) {
                    root.add(new TreeNode(v));
                }
                return root;
            }
        }
        
        const root = TreeNode.from(13, 4, 9, 16);
        console.log(root);
        console.log('Tilt at root = ', root.tilt());
        .as-console-wrapper { max-height: 100% !important; top: 0; }

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-12-24
          • 1970-01-01
          • 2015-04-08
          • 2010-12-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多