【问题标题】:How to count the number of right children in a binary tree?如何计算二叉树中右孩子的数量?
【发布时间】:2010-04-14 04:30:27
【问题描述】:

如何计算二叉树的右孩子个数?

这意味着我只希望孩子们被标记为正确的。

例如。

(Left | Right)

      F(Root)    
  G   |   H     
T   U |  I  J  

正确的孩子应该是 U、H 和 J。

找到这些的算法是什么。

【问题讨论】:

  • 你的意思是“孩子”还是“正统的后代”?因为如果是前者,要么是 1,要么是 0。

标签: algorithm data-structures binary-tree


【解决方案1】:
int count(Tree *r){
    if(r == NULL) return 0;
    int num_l=0, num_r=0;
    if(r->left != NULL) 
        num_l = count(r->left);
    if(r->right != NULL) 
        num_r = count(r->right)+1;
    return num_l+num_r
}

【讨论】:

  • 您没有处理这种情况 r == NULL 如果我将 NULL 传递给您的函数,它将崩溃。
【解决方案2】:

在递归方法中,

你会调用一个函数来遍历你的树, 对于当前节点,您需要: 检查当前节点是否有右子节点(然后递增计数器),然后为右节点递归调用该函数。 检查当前节点是否有左子节点,递归调用左节点函数。

这应该可行。

【讨论】:

    【解决方案3】:

    在树上做一个简单的遍历(即按顺序排序),如果每个节点有右子节点,则 +1。

    示例(没试过编译检查):

    int countRightChildren(Node root)
    {
       if (root == null) return 0;
       int selfCount =  (root.getRightChild() != null) ? 1 : 0;
       return selfCount + countRightChildren(root.getLeftChild()) + countRightChildren(root.getRightChild());
    }
    

    【讨论】:

      【解决方案4】:

      你可以递归地这样做:

      • 如果树不存在,则不存在 R 孩子。
      • 如果树存在,那么#R children =# R子树中的R孩子+#R L子树中的孩子

      .

        int countRChildren(Node *root) {
              if(!root)  // tree does not exist.
                  return 0;
      
              // tree exists...now see if R node exits or not.
              if(root->right) // right node exist
      
                  // return 1 + # of R children in L/R subtree.
                  return 1 + countRChildren(root->right) + countRChildren(root->left);
      
              else // right nodes does not exist.
                  // total count of R children will come from left subtree.
                  return countRChildren(root->left);
          }
      

      【讨论】:

        【解决方案5】:

        这包括我如何构建结构

         struct Item
         {
           int info;
           struct Item* right;
           struct Item* left;
         };
         typedef struct Item* Node;
        
        int countRightSons(Node tree)
        {
          if(!tree)
            return 0;
          if(tree->right != NULL)
            return 1 + countRightSons(tree->right) + countRightSons(tree->left);
           return countRightSons(tree->left);
        }
        

        【讨论】:

          【解决方案6】:

          简单的递归方法, 检查(即使不需要)所有 4 种可能性:

          1. 左右不存在

          2. 左右存在

          3. 左存在右不存在

          4. 右存在左不存在

             public static int countRightChildren(BST tree) {
                 if (tree.root==null) return Integer.MIN_VALUE;
                 return countRightChildren(tree.root);}
            
            
             public static int countRightChildren(Node curr) {
            
             if (curr.right==null&&curr.left==null) return 0;
             else if (curr.right!=null&&curr.left==null) 
                 return curr.right.data+countRightChildren(curr.right);
             else if (curr.right==null&&curr.left!=null)
                 return countRightChildren(curr.left);
             else if (curr.right!=null&&curr.left!=null) 
                 return curr.right.data+countRightChildren(curr.left)+countRightChildren(curr.right);
            
             return Integer.MIN_VALUE;
             }
            

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2023-03-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-04-28
            • 1970-01-01
            • 1970-01-01
            • 2012-03-02
            相关资源
            最近更新 更多