Does anyone have an idea as to how I could recreate this:
The end objective is to traverse the tree and count every endpoint. In this case 3 because 1, 3, 2 are all endpoints.
Does anyone have an idea as to how I could recreate this:
The end objective is to traverse the tree and count every endpoint. In this case 3 because 1, 3, 2 are all endpoints.
If you don't want to use simple lists, you could build a basic class. Something like:
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)
And then add whatever other methods you want.