This might be some python language thing but I am not able to wrap around my head on this.
When putting values in children of one TreeNode object makes children of other objects point to the children list of original object.
def createTree(parent, value):
nmap = {}
for i, v in enumerate(value):
nmap[i] = TreeNode(v)
for i, v in enumerate(parent):
if v is not -1:
print("adding child for node: " + str(v))
nmap[v].children.append(nmap[i])
else:
print("Not adding")
class TreeNode:
val = None
children = []
def __init__(self, v):
self.val = v
createTree([-1,0,0,1,2,2,2],[0,1,2,3,4,5,6])