【问题标题】:Counting left-child nodes in a Tree计算树中的左子节点
【发布时间】:2011-03-21 17:58:12
【问题描述】:

我应该实现一个计算左子树节点数量的递归方法。到目前为止我的代码是:

private int countLeftNodes(IntTreeNode node){
    int c = 0;
    if (node != null){
        c = 1 + countLeftNodes(node.left);
        countLeftNodes(node.right);
    }

    return c;
}

它返回的数字远小于应有的数字。我感觉我的遍历已经关闭,因为它似乎只计算最左边的子节点,然后终止。当我在大小为 16 的 IntTree 上调用此方法时,我应该得到 8 个左子节点、7 个右子节点和一个根,但我得到了 4 个左子节点。

【问题讨论】:

  • 你没有添加右孩子的左节点。

标签: java data-structures tree nodes


【解决方案1】:

你永远不会计算右树中的左节点。

private int countLeftNodes(IntTreeNode node)
{
    int c = 0;
    if (node.left != null)
    {
        c += 1 + countLeftNodes(node.left);
    }
    if(node.right != null)
    {
        c += countLeftNodes(node.right);
    }

    return c;
}

【讨论】:

  • 我试过了,但是它返回整个树中的节点数量。
  • 是的,我明白为什么,等一分钟,我会修复
  • @Shane 现在应该可以工作了。如果左孩子不为空,它基本上会向前看并添加一个。
  • 您可能需要添加检查以查看节点本身是否为空,以解决空根节点的情况。
  • 天啊!是的,我现在看到了。以前怎么想不到!非常感谢!
【解决方案2】:

要计算左子节点,您可以这样做:

private int countLeftNodes(IntTreeNode node) {

    // no tree no left-child nodes      
    if(node == null) {
       return 0;
    }

    // left-child count of current node.
    int c = 0;

    // does the current node have a left-child ?
    if (node.left != null){
      c = 1;
    }

    // return left-child count of current node +
    // left-child count of left and right subtrees
    return c + countLeftNodes(node.left) + countLeftNodes(node.right);
}

【讨论】:

    【解决方案3】:
    private int countLeftNodes(IntTreeNode node){
        int c = 0;
        if (node != null){
            if(node.left!=null) {
               c = 1 + countLeftNodes(node.left);
            }
            if(node.right!=null){
                c +=countLeftNodes(node.right);
            }
        }
    
        return c;
    }
    

    【讨论】:

      【解决方案4】:

      最容易检查的地方是在父级中。

      private int countLeftNodes(IntTreeNode node){
      
          int c = 0;
          if(node.left != null)
          {
              c++; 
              c+= countLeftNodes(node.left)
          }
          if(node.right != null)
          {
              c+= countLeftNodes(node.right);
          }
      
          return c;
      }
      

      【讨论】:

        【解决方案5】:

        使用递归时我最喜欢的风格是使用某种包装函数,其中主要方法调用另一个执行 grunt 工作的方法:

        private int countLeftNodes(IntTreeNode node){
           int totalCount = reallyCountLeftNodes(IntTreeNode node, 0); 
           return totalCount;
        }
        
        private int reallyCountLeftNodes(IntTreeNode n, Int sum){
            if (n.left == NULL && n.right == NULL){  //check if we've hit rock bottom
                return sum;
            } else if (n.left == NULL) { //if the node's left is nil, go right
                reallyCountLeftNodes(n.right, sum++);   
            } else {
                reallyCountLeftNodes(n.left, sum++);  // Going as far left as possible!
            }
        }
        

        注意主函数如何调用另一个函数。我发现这种风格更干净,更容易理解。此外,第二个函数有一个计数变量供您使用。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-15
          • 2013-02-12
          • 1970-01-01
          • 2015-08-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多