我想完成一个function,他的要求如下
请问代码该怎么写?
```python
class Node:
def __init__(self, colour):
self.colour = colour
self.children = []
def add_child(self, child):
self.children.append(child)
class Colour:
GREEN = 'G'
RED = 'R'
class Tree:
def __init__(self, start_node):
self.start_node = start_node
def is_coloured_to_depth_k(self, start_node: Node, colour: Colour, k: int) -> bool:
if k == 0:
return start_node.colour == colour
if start_node.colour != colour:
return False
for child in start_node.children:
if not self.is_coloured_to_depth_k(child, colour, k - 1):
return False
return True
# 构建示例树
start = Node(Colour.GREEN)
g1 = Node(Colour.GREEN)
g2 = Node(Colour.GREEN)
r = Node(Colour.RED)
g1.add_child(g1)
g1.add_child(r)
start.add_child(g1)
start.add_child(g2)
r.add_child(r)
# 创建树对象
tree = Tree(start)
# 执行示例测试
print(tree.is_coloured_to_depth_k(start, Colour.GREEN, 0)) # True
print(tree.is_coloured_to_depth_k(start, Colour.RED, 0)) # False
print(tree.is_coloured_to_depth_k(start, Colour.GREEN, 1)) # True
print(tree.is_coloured_to_depth_k(start, Colour.GREEN, 2)) # False
```
此代码基于你提供的信息,并且假设树是通过Node对象的层次结构表示的。你可以根据实际情况进行适当调整和扩展。