Given a Tree structure:
class Node:
def __init__(self, data, left=None, right=None):
self.left = left
self.right = right
self.data = data
I want to be able to take the results(self.data) of the recursive calls of
def preOrder(self):
if self:
print(self.data)
if self.left is not None:
self.left.preOrder()
if self.right is not None:
self.right.preOrder()
and be able to return them in a single list.
Where
root= Node(2,Node(1,Node(6),Node(3)),Node(3,None,Node(9)))
root.preOrder()
would yield [2,1,6,3,3,9].
I have tried messing around with helper function but I am stuck and haven't been able to get a working solution.