Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
public class PracticalTestQ3 {
    
     public static class TreeNode
    {
        int data;
        TreeNode right;
        TreeNode left;
        
        TreeNode(int data)
        {
            this.data=data;
            left = right = null;
        }
    }
      
    // Recursive Solution
    /* To get the count of leaf nodes in a binary tree*/
    int getLeafCountOfBinaryTree(TreeNode node)
    {
        if(node == null)      
            return 0;
        if(node.left ==null && node.right==null)    
            return 1;          
        else
            return getLeafCountOfBinaryTree(node.left)+ getLeafCountOfBinaryTree(node.right);    
    }
    public static void main(String[] args)
    {
        // Creating a binary tree
        TreeNode rootNode=createBinaryTree();
        System.out.println("Number of leaf nodes in binary tree :" + TreeNode.getLeafCountOfBinaryTree());
    }
 
    public static TreeNode createBinaryTree()
    {
        
        //create a tree 
        BinaryTree tree = new BinaryTree();
        TreeNode rootNode =new TreeNode(35);
        TreeNode node20=new TreeNode(20);
        TreeNode node50=new TreeNode(50);
        TreeNode node10=new TreeNode(10);
        TreeNode node30=new TreeNode(30);
        TreeNode node40=new TreeNode(40);
        TreeNode node60=new TreeNode(60);
        TreeNode node25=new TreeNode(25);
        TreeNode node55=new TreeNode(55);
 
        rootNode.left=node20;
        rootNode.right=node50;
 
        node20.left=node10;
        node50.left=node40;
        node30.left=node25;
        node60.left=node55;
 
        return rootNode;
    }
 }


What I have tried:

a lot of things i already try such as changing the int and so on but i still dont get the correct answer. I got error on this line
// Creating a binary tree
        TreeNode rootNode=createBinaryTree();
        System.out.println("Number of leaf nodes in binary tree :" + TreeNode.getLeafCountOfBinaryTree());
Posted
Updated 20-Dec-22 8:09am
v3
Comments
OriginalGriff 19-Dec-22 2:28am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.
Richard MacCutchan 19-Dec-22 4:03am    
Firstly this is not netbeans code, it is Java. The actual problem as far as I can see is that you have not declared (or imported) a BinaryTree class.
Dave Kreskowiak 19-Dec-22 9:21am    
...and the error message would be?? Hint: The most important piece of information you can supply when asking questions about problems with your code!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900