I'm trying to make preOrder recursive so that I can do something such as:
Given the array representation of a tree [2,1,3] (the program actually takes a TreeNode, specifically TreeNode(2)
x = preOrder(root)
print x.next() # 1
print x.next() # 2
print x.next() # 3
But given the following code, I'm only able to call x.next() once, then it nothing returns after the first iteration.
def preOrder(root):
if root is not None:
preOrder(root.left)
self.lChild = root.left
self.rChild = root.right
yield root.val
preOrder(root.right)
How can I fix this?
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None