Skip to content

Instantly share code, notes, and snippets.

@anil477
Created June 30, 2017 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/9d94dbb77ae80c9c0c5b7e420f4037ac to your computer and use it in GitHub Desktop.
Save anil477/9d94dbb77ae80c9c0c5b7e420f4037ac to your computer and use it in GitHub Desktop.
Count of leave nodes in Binary Tree
class Node {
int data;
Node left, right;
public Node(int item) {
data = item;
left = right = null;
}
}
class BinaryTree {
Node root;
public void printleaveNode() {
System.out.println(" Leave Node Count: " + leaveNode(root));
}
public int leaveNode(Node root){
if(root==null){
return 0;
}
if(root.left == null && root.right == null ) {
return 1;
}
return leaveNode(root.left) + leaveNode(root.right);
}
public static void main(String args[]) {
BinaryTree tree = new BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.printleaveNode();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment