【问题标题】:Understanding how to calculate the depth of a Binary Tree了解如何计算二叉树的深度
【发布时间】:2014-07-22 22:28:17
【问题描述】:

我正在将职业杯指南作为速成课程,通过基本的 CS 原则,并坚持计算二叉树的最小/最大深度的示例。由于这是我在几乎每个示例中遇到的相同问题,因此我认为我会在这里发布一个问题。

这些指令是实现一个检查树是否平衡的方法。为了做到这一点,您需要比较最小深度和最大深度,并确保它们的差不大于 1。这个原理非常简单。第 15 行的方法就是为了做到这一点。

但是,我不明白每个辅助方法(maxDepthminDepth)的返回语句中发生了什么。如何从root.leftroot.right 派生一个数字? Math.max 函数是否只是假设 10 是否存在 value/null 节点,或者,由于没有指定值(只是 Node 对象),Math.max(maxDepth(root.left), maxDepth(root.right) 本身是否等于 @ 987654330@,从而将返回值增加1,直到两个节点都为空?

如果是这样,这是用于计算树的最小/最大深度的一般过程:

minDepth = 根有孩子吗? yes = minDepth = 1,no = minDepth = 0(如果有根)

maxDepth = 循环遍历两个分支,直到找到离根最远的叶子。保持一个计数器来确定叶子。

1   public static int maxDepth(TreeNode root) {
2       if (root == null) {
3       return 0;
4       }
5       return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
6   }
7
8   public static int minDepth(TreeNode root) {
9       if (root == null) {
10          return 0;
11      }
12      return 1 + Math.min(minDepth(root.left), minDepth(root.right));
13  }
14
15  public static boolean isBalanced(TreeNode root){
16      return (maxDepth(root) - minDepth(root) <= 1);
17  }

【问题讨论】:

    标签: java c++ binary-tree binary-search-tree


    【解决方案1】:

    maxDepth(root.left) 返回左子树的最大深度。
    maxDepth(root.right) 返回右子树的最大深度。
    这两个中的最大值是最大的子树深度。
    根节点加 1,得到树的最大深度。

    假设这是树:

                A
             B      C
           D   E   F   G
         H
       I
    

    只要看一下就可以看到最大深度为 5(由路径 A-B-D-H-I 形成),最小深度为 3(由多个路径形成,例如 A-C-G)。

    现在,最大深度为 1(对于根 A)+ 两个子树的最大深度。
    第一个以 B 为根的子树的最大深度为 4 (B-D-H-I)。根为 C 的第二个子树的最大深度为 2 (C-F)。
    最大(4,2)= 4
    因此整棵树的最大深度为 1 + max(4,2) = 5。

    如果我们使用示例树中的字母来表示以这些节点为根的子树,我们会得到:

    maxDepth(A) = 1 + max(maxDepth(B) , maxDepth(C)) =
                  1 + max(1 + max(maxDepth(D) , maxDepth(E)), 1 + max(maxDepth(F) , maxDepth(G)) =
                  1 + max(1 + max(1+max(maxDepth(H),0) , 1+max(0,0)), 1 + max(1+max(0,0) , 1+max(0,0)) =
                  1 + max(1 + max(1+max(1+max(maxDepth(I),0),0) , 1), 1 + 1) =
                  1 + max(1 + max(1+max(1+max(1+max(0,0),0),0) , 1), 1 + 1) =
                  1 + max(1 + max(1+max(1+max(1,0),0) , 1), 2) =
                  1 + max(1 + max(1+max(2,0) , 1), 2) =
                  1 + max(1 + max(3 , 1), 2) =
                  1 + max(4, 2) =
                  1 + 4 =
                  5
    

    类似地,对于计算最小深度,你计算两个(左和右)子树的最小深度,取两者中的最小值,并为根加 1。

    【讨论】:

    • 这就是我不关注的:maxDepth(root.left) 如何返回树的深度?
    • @NealR maxDepth(root.left) 返回子树的最大深度,其根为原始树根的左子树。如果您不熟悉递归,则需要时间来适应它(并相信它确实有效)。
    • 抱歉,这里有点笨拙,但是这些数字(最大深度)是如何计算的?
    • 我被困在程序如何递增它用作计数器的任何内容
    • 一直沿着树的左侧向下遍历。当您到达叶节点时,将高度返回为 1。父节点将在该数字上加 1。最终,您的遍历将返回到树的一条路径高度的顶部。
    【解决方案2】:

    嗯,这是一个递归函数,它检查树的两边,然后将结果与最大值或最小值进行比较。

    图片帮助。

           o  go left and right
         /    \
    (+1)o      o(+1)       
        \       
         o(+1)   
    

    所以让我们把它带回到代码中。

    return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));

    调用堆栈看起来像这样

    // max return 2
    - left +1                      
        // max return 1
        -left 0                    
        -right +1
           // max return 0
          -left  0                 
          -right 0
    - right +1
        //max return 0
        -left 0
        -right 0
    

    Min 的工作方式基本相同。

    【讨论】:

      猜你喜欢
      • 2010-12-24
      • 2022-01-25
      • 2010-12-24
      • 2013-02-19
      • 1970-01-01
      • 2010-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多