public static int numberOfLeavesInBT(BinaryTreeNode root){
if(root == null)
return 0;
Queue queue = new LinkedList();
queue.add(root);
int count =0;
while(!queue.isEmpty()){
BinaryTreeNode node = queue.poll();
if(node.getRight() == null&& node.getLeft() ==null)
count++;
if(node.getLeft() !=null)
queue.add(node.getLeft());
if(node.getRight() !=null)
queue.add(node.getRight());
}
return count;
}
Thursday, July 6, 2017
Number of leaves in Binary Tree
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment