class TreeNode:
def __init__(self, value):
self.value = value
self.children = []
def print_tree(node, indent=0):
print(" " * indent + str(node.value))
for child in node.children:
print_tree(child, indent + 1)
def ceate_tree():
A, B, C, D, E, F = [TreeNode(x) for x in 'ABCDEF']
A.children = [B, D]
B.children = [C]
D.children = [E, F]
return A
if __name__ == '__main__':
root = ceate_tree()
print_tree(root)
输出
A
B
C
D
E
F