【问题标题】:Confusion regarding height of binary tree关于二叉树高度的混淆
【发布时间】:2014-10-31 04:25:37
【问题描述】:

假设我的树看起来像这样

     5
   /  \
  1    8 
      /  \
     6    9

我写了一个程序来找到这个的高度。

     65 int height(const TNODE* root) {                                                                                                                                                                                                           
 66         if (root == NULL) {                                                                                                                                                                                                               
 67                 return 0;                                                                                                                                                                                                                 
 68         } else {                                                                                                                                                                                                                          
 69                 int lDepth = height(root->left);                                                                                                                                                                                          
 70                 int rDepth = height(root->right);                                                                                                                                                                                         
 71                 if (lDepth > rDepth) {                                                                                                                                                                                                    
 72                         return (lDepth + 1);                                                                                                                                                                                              
 73                 } else {                                                                                                                                                                                                                  
 74                         return (rDepth + 1);                                                                                                                                                                                              
 75                 }                                                                                                                                                                                                                         
 76                                                                                                                                                                                                                                           
 77         }                                                                                                                                                                                                                                 
 78 }

运行此方法会为上面的树打印 3。但是高度不应该是 2,因为这是从根到最远叶子的边数?我在网上搜索过,我得到了相互矛盾的信息。有人会说我上面的树的高度是 3,而其他人说是 2。

那么我上面的树应该是什么高度,为什么?

我的方法对吗?

【问题讨论】:

  • 这显然取决于您的应用程序。根据您的应用选择符合您期望的。
  • 到底是什么问题?
  • @igon 我已经添加了一些说明。
  • 2 边高度和总是 2 + 1 节点高度。这取决于您计算节点还是边。

标签: c binary-tree


【解决方案1】:

“身高”这个词在这里有点太笼统了。 2 或 3 都可能是正确答案,具体取决于确切的定义。

例如,如果我说树的高度是它包含的“级别”数,那么 3 就是正确答案。但是,如果我说树的高度是到达叶子的最大边数,并且不超过一次(即没有回溯),那么 2 是正确的答案。

根据我的经验,更常见的答案是 3。这棵树有 3 个级别。只有一个根节点的树的高度为 1。我更喜欢这样,因为空树的高度为 0。

【讨论】:

    【解决方案2】:

    只是关于样式的建议。

      int height(const TNODE* root) {                                                                                                                                                                                                           
        if (root == NULL) {                                                                                                                                                                                                               
           return 0;                                                                                                                                                                                                                 
        } else {                                                                                                                                                                                                                          
          int lDepth = height(root->left);                                                                                                                                                                                          
          int rDepth = height(root->right);  
          return max ( lDepth, rDepth )  + 1;                                                                                                                                                                                                   
         }                                                                                                                                                                                                                                 
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多