I have this method:

static int countLeaves(BinaryNode node) {
        if (node == null) {
            return 0;
        } 
        else if (node.left == null && node.right == null) {
            return 1;
        }
        return countLeaves(node.left) + countLeaves(node.right);
}

and i want to Write a client program that constructs a binary search tree whose elements are given by the user. Then, find and print the number of leaf nodes in the constructed tree.

I need ur help plz :)

Then create instances of the BinaryNode class and construct the tree by giving values to the .left and .right nodes. Then call that method with argument the root node.

For reading input use the Scanner class

aha Thnx :)
but plz do me a favor and check my code from errors

import java.util.*;
public class ClientCodeTest
{
  static Scanner console=new Scanner (System.in);
  
    public static void main(String[] args)
	 {
	    BinarySearchTree testTree=new BinarySearchTree();
		 int num,count;
 System.out.println("Enter number of  elements to build your tree");
		 num=console.nextInt();
		 while(num!=null)
		 {
		    tree.add(num);
		    num=console.nextInt();
		  }
		  
		count=leafCount(tree);
		System.out.print("The number of Leaves in your tree is:");
		System.oyt.println(count);
		
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.