I have a random binary tree in the following forms
12
13, 14
29, 26, 89
Each node has two children i.e ( 12->(13, 14), 13->(29, 26), 14 ->(26, 89)). Here I need to return all possible paths in the forms [ [12, 13, 29], [ 12, 13, 26], [12, 14, 26], [12, 14, 89]]. I tried with the following code. I have problem with updating list. Thanks in advance.
class Tree:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def __str_(self):
return '%s' % self.data
def makeList(tree, path =[]):
if(tree != None):
path.append(tree.data)
if tree.left:
path.append(makeList(tree.left, path))
if tree.right:
path.append(makeList(tree.left, path))
return path
root = Tree(12)
root.left = Tree(13)
root.right = Tree(14)
root.right.left = Tree(26)
root.left.right = Tree(26)
root.left.left = Tree(29)
root.right.right = Tree(86)
x = makeList(root)
26
is connected both to13
and14
this is not a binary tree! (in a binary tree each node has, at most, one parent). – Nir Alfasi