【问题标题】:Determine number of nodes in a binary tree based on their number of children根据子节点数确定二叉树中的节点数
【发布时间】:2020-09-12 19:24:54
【问题描述】:

给你一个二叉树的根节点T。我们在 T 中区分三种类型的节点:具有 0 个子节点(即叶子)的节点、具有 1 个子节点的节点和具有两个子节点的节点。为每种类型确定T 中的节点总数。将结果作为长度为 3 的整数数组返回。

输出应该是一个包含上述所有类型节点计数的数组。

这是我拥有的,但在我运行它时没有通过我的导师的测试:

private static int[] problem1(Node root){

     int[] nodeCount = new int[]{0,0,0};

     // BASE CASE
     if (root == null) {

        // Implement me!
        return new int[] {
           -1, // nodes with 0 children
           -1, // nodes with 1 child
           -1  // nodes with 2 children
        };

     }

     // RECURSIVE CALL
     int[] leftChild = problem1(root.left);
     int[] rightChild = problem1(root.right);

     nodeCount[0] = leftChild[0] + rightChild[0];
     nodeCount[1] = leftChild[1] + rightChild[1];
     nodeCount[2] = leftChild[2] + rightChild[2];

     if(root.left != null && root.right != null) {

        nodeCount[2]++;
        problem1(root.left);
        problem1(root.right);

     }else if(root.left != null && root.right == null) {

        nodeCount[1]++;
        problem1(root.left);

     }else if(root.left == null && root.right != null) {

        nodeCount[1]++;
        problem1(root.right);

     }else {

        nodeCount[0]++;
     }

     return nodeCount;
}

【问题讨论】:

  • 欢迎来到 Stackoverflow!如果您需要特定语言的帮助,请为您的问题添加更多详细信息,并标记您使用的语言。 Stackoverflow 不仅仅是让其他人做功课的地方。请提供一些您将如何解决问题的示例,也许其他人可以帮助填补空白。 stackoverflow.com/help/how-to-ask
  • @ptpatersin - 我的导师编写了所有代码来测试这个方法,我们不允许改变它,不能有任何帮助方法来制作这个更干净的代码,所以我什至没有知道从哪里开始
  • @DevChauhan:感谢您想在这里编辑问题。不过,我已经恢复了您的编辑-您编辑的语句显然是从作业问题中复制粘贴的。这些应该保持不变,因为这正是作者正在阅读的问题。
  • 嗨,杰。我们不会在这里将问题变成答案,因为对于未来的读者来说问题所在是没有意义的。感谢您想添加答案-您可以在下面添加吗?点击“回答您的问题”按钮。
  • 你的代码有一个注释“implement me”,意思是它不完整,因为有一部分你还没有实现。

标签: java binary-tree nodes


【解决方案1】:

您的代码的问题是每次调用空节点时数组的每个元素都是-1。此代码将给出正确答案:

private static int[] problem1(Node root){
    // Implement me!

    //initialize arrays
    int[] nodeCount = {0,0,0};
    int[] n1 = new int[3];
    int[] n2 = new int[3];

    //2 children case
    if(root.left != null && root.right != null){
        n1 = problem1(root.left);
        n2 = problem1(root.right);
        nodeCount[0] += n1[0] + n2[0];
        nodeCount[1] += n1[1] + n2[1];
        nodeCount[2] += n1[2] + n2[2];
        nodeCount[2]++;
        return nodeCount;           
    }

    //leaf case
    if(root.left == null && root.right == null){
        return new int[] {1,0,0};
    }

    //only right child 
    if (root.left == null && root.right != null){
        n1 = problem1(root.right);
        nodeCount[0] += n1[0];
        nodeCount[1] += n1[1];
        nodeCount[2] += n1[2];
        nodeCount[1]++;
        return nodeCount;
    }

    //only left child
    if (root.left != null && root.right == null){
        n1 = problem1(root.left);
        nodeCount[0] += n1[0];
        nodeCount[1] += n1[1];
        nodeCount[2] += n1[2];
        nodeCount[1]++;
        return nodeCount;
    }

   //should never run unless null node is given at start  
   else{
       return new int[] {
           -1, // nodes with 0 children
           -1, // nodes with 1 child
           -1  // nodes with 2 children
       };
    }
}

【讨论】:

    【解决方案2】:

    我正在写粗略的代码,请检查它是否解决了您的问题:

        HashMap<Integer, Integer> count = new HashMap<>();
    
        // For now, 0 key having a count of leaf nodes, 1 key count of nodes having a single child, 2 for both 
        // Use inorder, pre or post to traverse the tree... use countNodes to get count
        // I have used hashmap to keep count, you can take an array as well.
    
       public void inorder(TreeNode root){
           if(root == null) return;
           inorder(root.left);
           countNodes(root);
          inorder(root.right);
        }
    
      public void countNodes(TreeNode root){
           if(root == null) return;
           else if(root.left != null && root.right != null){
             count.put(2, 1 + count.getOrDefault(2,0));
           } 
          else if(root.left == null && root.right == null){
             count.put(0, 1 + count.getOrDefault(0,0));
           } 
          else{
             count.put(1, 1 + count.getOrDefault(1,0));
          } 
        }
    

    【讨论】:

    • 我还是个初学者,所以我还没有了解哈希映射,而且我的导师不允许任何辅助方法使这个方法更干净,所以很遗憾这不起作用,但感谢您的输入!
    猜你喜欢
    • 1970-01-01
    • 2016-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多