答案 0 :(得分:1)
如果您不想使用简单的列表,则可以构建一个基本类。像这样:
class NonBinTree:
def __init__(self, val):
self.val = val
self.nodes = []
def add_node(self, val):
self.nodes.append(NonBinTree(val))
def __repr__(self):
return f"NonBinTree({self.val}): {self.nodes}"
a = NonBinTree(0)
a.add_node(1)
a.add_node(3)
a.add_node(4)
a.nodes[2].add_node(2)
print(a)
然后添加所需的其他任何方法。