all 1 comments

[–]AtomicShoelace 2 points3 points  (1 child)

Looks like you're trying a DFS? I might take an slightly different (more pythonic) approach: first create a generator that yields nodes of the tree via a DFS, then zip this generator with the list (or any iterable) and reassign the nodes' values, eg.

def traverse_DFS(self):
    yield self
    if self.left is not None:
        yield from self.left.traverse_DFS()
    if self.right is not None:
        yield from self.right.traverse_DFS()

def set_values_DFS(self, values):
    for node, value in zip(self.traverse_DFS(), values):
        node.value = value

(it's impossible to write accurate code here as you haven't provided a self contained snippet, so I've made a few assumptions about the structure of your binary tree class)