Created
June 30, 2017 13:50
-
-
Save anil477/9d94dbb77ae80c9c0c5b7e420f4037ac to your computer and use it in GitHub Desktop.
Count of leave nodes in Binary Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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