Yahoo Interview Question

Write a program to count the number of leaves in a binary tree.

Interview Answer

Anonymous

Dec 5, 2011

def count_leaves(root): if not root: return 0 elif not root.left and not root.right: return 1 else: return count_leaves(root.left) + count_leaves(root.right)

1