Algorithm 我需要设计一个算法来返回二叉树中有两个子节点的节点数

Algorithm 我需要设计一个算法来返回二叉树中有两个子节点的节点数,algorithm,data-structures,Algorithm,Data Structures,我遇到这个问题是为了设计一个算法来计算二叉树中有两个子树的节点数。有人提到,解决方案应表示为一对函数(而不是BST成员函数) 到目前为止,我还不能得出一个具体的解决方案,特别是解决方案应该表示为一对非BST成员函数的那部分,我感到非常困惑 //计算具有2个子节点的节点数 函数countNodes(节点元素、节点枚举器){ var nodeNumber=0; var children=nodeElement.children; 对于(var c=0;c假设节点是一个结构,包含两个指向节点的指针,分

我遇到这个问题是为了设计一个算法来计算二叉树中有两个子树的节点数。有人提到,解决方案应表示为一对函数(而不是BST成员函数)

到目前为止,我还不能得出一个具体的解决方案,特别是解决方案应该表示为一对非BST成员函数的那部分,我感到非常困惑

//计算具有2个子节点的节点数
函数countNodes(节点元素、节点枚举器){
var nodeNumber=0;
var children=nodeElement.children;

对于(var c=0;c假设
节点
是一个
结构
,包含两个指向
节点
的指针,分别命名为

int count_2_ch_nodes(Node* root)
{
    if (root == NULL) return 0;
    // Recursively count the number of nodes that are below
    // this node that have two children:
    int count = 0;
    if (root->left != NULL) count += count_2_ch_nodes(root->left);
    if (root->right != NULL) count += count_2_ch_nodes(root->right);
    // Add this node IF it has 2 children:
    if (has_2_ch(root)) count++;
    return count;
}

/* Returns TRUE if node has two children */
int has_2_ch(Node* node)
{
    return (node->left != NULL && node->right != NULL);
}

这是您的问题的完整java代码

import java.util.ArrayList;
import java.util.Scanner;


/*
*  Creating datastructure for Node
*  Every Node contains data given by user and leftChild and rightChild childs
*  Left and rightChild childs are automatically assigned by the program
*/
class Node
{
    Node leftChild, rightChild;
    String data;

    /*
     *  Assigning leftChild , rightChild and data to the node using a constructor
     */

    Node(Node left, Node right, String data)
    {
        this.leftChild =left;
        this.rightChild =right;
        this.data=data;
    }

}
public class FirstAnswer {
    /*
     * Initializing the count for number of nodes
     */
    private static int count=0;
    private static int numberOfNodes(Node root)
    {

        /*
         * Writing the base case for the recursive function
         * If leftChild or rightChild or both are null it returns 0 as no childs are present
         */

        if ((root.leftChild ==null && root.rightChild ==null) || (root.leftChild ==null) || (root.rightChild ==null))
            return 0;

        else {

            count+=2;
            Node left=root.leftChild;
            Node right=root.rightChild;

            /*
             *  Calling the recursive function twice by making leftChild child and rightChild child of root as root
             */

            System.out.println(root.data+" : "+"\n"+"Left child : "+left.data+"\n"+"Right child : "+right.data);
            numberOfNodes(left);
            numberOfNodes(right);

        }
        return count+1; //Since root node is not counted
    }
    public static void main(String... args)
    {
        Scanner sc=new Scanner(System.in);

        /*
         *  Creating individual nodes with string data from user
         *  Holding them in an array list inputs
         */

        ArrayList<Node> inputs=new ArrayList<>();
        String status="Y";
        System.out.print("Enter data for root node : ");
        inputs.add(new Node(null,null,sc.next()));
        while (status.equals("Y") || status.equals("y"))
        {
            if (inputs.size()%2==1)
            {
                for (int j=0;j<2;j++)
                {
                    System.out.print("data for child "+(j+1)+" : ");
                    inputs.add(new Node(null,null,sc.next()));
                }

                /*
                 *  Yes or No for adding more number of nodes
                 */

                System.out.println("Press Y or y if more inputs have to be given else press N to construct tree with given inputs...");
                status=sc.next();
            }
        }
        Node[] tree=new Node[inputs.size()];
        /*
         *   Above is the tree which is being constructed from the nodes given by user
         */
        for (int i=inputs.size()-1;i>=0;i--)
        {
            int j=i+1;
            /*
             *   Making tree format by locating childs with indices at 2*p and 2*p+1
             */

            if ((2*j+1)<=inputs.size())
            {
                tree[i]=new Node(tree[2*i+1],tree[2*i+2],inputs.get(i).data);
            }
            else {
                tree[i]=inputs.get(i);
            }

        }

        /*
         *   Calling the recursive function to count number of nodes
         *   Since first node is the root we start from here
         */

        System.out.println(numberOfNodes(tree[0]));
    }
}
import java.util.ArrayList;
导入java.util.Scanner;
/*
*为节点创建数据结构
*每个节点都包含用户以及leftChild和rightChild提供的数据
*左、右子代由程序自动分配
*/
类节点
{
节点leftChild,righchild;
字符串数据;
/*
*使用构造函数将leftChild、rightChild和数据分配给节点
*/
节点(左节点、右节点、字符串数据)
{
this.leftChild=left;
this.rightChild=right;
这个。数据=数据;
}
}
公共类第一答案{
/*
*正在初始化节点数的计数
*/
私有静态整数计数=0;
私有静态int numberOfNodes(节点根)
{
/*
*编写递归函数的基本情况
*如果leftChild或rightChild或两者都为null,则返回0,因为不存在任何child
*/
if((root.leftChild==null&&root.rightChild==null)| |(root.leftChild==null)| |(root.rightChild==null))
返回0;
否则{
计数+=2;
Node left=root.leftChild;
Node right=root.rightChild;
/*
*通过将根的leftChild子级和rightChild子级作为根来调用递归函数两次
*/
System.out.println(root.data+“:“+”\n“+”左子项:“+Left.data+”\n“+”右子项:“+Right.data”);
节点数(左);
节点数(右);
}
返回计数+1;//因为根节点未计数
}
公共静态void main(字符串…参数)
{
扫描仪sc=新的扫描仪(System.in);
/*
*使用来自用户的字符串数据创建单个节点
*将它们保存在数组列表输入中
*/
ArrayList输入=新的ArrayList();
字符串状态=“Y”;
System.out.print(“为根节点输入数据:”);
add(新节点(null,null,sc.next());
while(status.equals(“Y”)| status.equals(“Y”))
{
if(inputs.size()%2==1)
{
对于(int j=0;j=0;i--)
{
int j=i+1;
/*
*通过定位索引为2*p和2*p+1的child生成树格式
*/

if((2*j+1)到目前为止您尝试了什么?目前来看,这看起来像是一个家庭作业问题树*tobj;int count(Tree*root){if(root==NULL)返回0;else if(root->left==NULL&&root->right==NULL)返回1;else返回count(root->left)+count(root->right)+1;}当你提交你的解决方案时,别忘了信任这个网站。否则在学术上是不诚实的。我不理解这个问题,一棵二叉树有两个孩子(这就是为什么它是二叉树),对吗?还是我没有理解这个问题?这是一个JS函数,试着画一棵树,它会帮你大忙,伙计