Your task is to count the number of leaves in a binary tree. You may assume that the tree has at most 100 nodes.
The example in the code template represents the following tree with two leaves:
In a file leaves.py
, implement a function count
that returns the number of leaves.
from collections import namedtuple def count(node): # TODO if __name__ == "__main__": Node = namedtuple("Node",["left","right"]) tree = Node(None,Node(Node(None,None),Node(None,None))) print(count(tree)) # 2
Instructions on dealing with binary trees in Python can be found on the course Moodle area.