最佳答案
如果您不想使用简单的列表,您可以构建一个基本类。像这样的东西:
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)
然后添加您想要的任何其他方法。
关于python - Python 中的非二叉树数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60579330/