简体   繁体   中英

Count number of leaf nodes in a generic tree

I am quite sure that my logic is correct but I am getting a wrong answer. Kindly point out the mistake without changing the logic of my program.

public static int countLeaves(TreeNode<Integer> root,int max)
    {
// TODO Auto-generated method stub
        if(root==null)
        {
            return 0;
        }
        if(root.children.size()==0)
        {
            return 1;
        }
        
        for(int i = 0; i < root.children.size(); i++) 
        {
            max = max + countLeaves(root.children.get(i),max);
        }
        
        return max;
    }
public static void main(String[] args) {
        // TODO Auto-generated method stub

        TreeNode<Integer> root = takeInputLevelWise();
        printTree(root);
        int max = 0;
        
        System.out.println(countLeaves(root, max));
}

Your logic is not correct so I can't point out your error without changing your logic:-)

The issue is double-counting, here:

    for(int i = 0; i < root.children.size(); i++) 
    {
        max = max + countLeaves(root.children.get(i),max);
    }

That is, you're passing down max (which is the count of leaves so far), adding to it, then returning it, and adding again .

Think of it from a design perspective. Why should countLeaves even need to know your count so far? countLeaves should only return the number of leaves under it, right? There's no need for max to be passed down.

public static int countLeaves(TreeNode<Integer> root)
    {
// TODO Auto-generated method stub
        int leaves = 0;
        if(root==null)
        {
            return 0;
        }
        if(root.children.size()==0)
        {
            return 1;
        }

        for(int i = 0; i < root.children.size(); i++) 
        {
            leaves += countLeaves(root.children.get(i));
        }

        return leaves;
    }
public static void main(String[] args) {
        // TODO Auto-generated method stub

        TreeNode<Integer> root = takeInputLevelWise();
        printTree(root);

        System.out.println(countLeaves(root));
}

The code in Python for count number of leaf node in generic tree is

def leafNodeCount(tree):
if len(tree.children) == 0:
    return 1
Num = 0
for child in tree.children:
    Num += leafNodeCount(child)
return Num

I am quite sure that my logic is correct but I am getting a wrong answer. Kindly point out the mistake without changing the logic of my program.

public static int countLeaves(TreeNode<Integer> root,int max)
    {
// TODO Auto-generated method stub
        if(root==null)
        {
            return 0;
        }
        if(root.children.size()==0)
        {
            return 1;
        }
        
        for(int i = 0; i < root.children.size(); i++) 
        {
            max = max + countLeaves(root.children.get(i),max);
        }
        
        return max;
    }
public static void main(String[] args) {
        // TODO Auto-generated method stub

        TreeNode<Integer> root = takeInputLevelWise();
        printTree(root);
        int max = 0;
        
        System.out.println(countLeaves(root, max));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM