A path is a route that connects two nodes, and the length of a path is the number of steps on the route. Your task is to compute the sum of the lengths of all paths connecting two leaves of a tree.
In the code template example, there are three such paths:
You may assume that the tree has at most 100 nodes.
In a file treepath.py
, implement a function count
that returns the sum of the lengths.
from collections import namedtuple def count(node): # TODO if __name__ == "__main__": Node = namedtuple("Node",["left","right"]) tree = Node(Node(None,None),Node(Node(None,None),Node(None,None))) print(count(tree)) # 8