Problem 6: Little Blue Whale Installs Camera (0 pts)
Given a tree, the little blue whale needs to install camera on the node of the tree. Each camera on a node can monitor its parent node, child nodes and itself. In order to be able to monitor all nodes of the tree, please implement install_camera
, which calculates the minimum number of cameras that the little blue whale needs to install.
Note: the labels of nodes from input tree are all 0, and it's ok to modify it.
Hint: Use label to mark the node. For example, 0 for not monitored, 1 for camera, and 2 for monitored by camera in other node.
def install_camera(t):
"""Calculates the minimum number of cameras that need to be installed.
>>> t = Tree(0, [Tree(0, [Tree(0), Tree(0)])])
>>> install_camera(t)
1
>>> t = Tree(0, [Tree(0, [Tree(0, [Tree(0)])])])
>>> install_camera(t)
2
"""
"*** YOUR CODE HERE ***"