i am trying to get a list of nodes (objetcs) in a python binary tree, i am looking for a recursive function implemented in the node object, so i will call function on the root node, and it will going down on childs nodes till reachs the specific level, and then will return those nodes in a list
My current aproach, i'm not sure if this is correct or the best way to implement it:
def get_level_nodes(self, nodes, level=1):
if self.level > level:
return nodes
if self.level == level:
nodes.append(self)
return nodes
for child in self.child_id:
nodes += child.get_level_nodes(node, level)
return nodes
# Getting the list
nodes_list = root_node.get_level_nodes([], 3)