darkpirate - 1 year ago 71

Python Question

I have a simple recursive function:

`def subTree(z,sublevels):`

if(z < sublevels):

print "from z = ", z, " to z = ", z+1

subTree(z+1,sublevels)

else:

print "z = ", z, " !"

this simply goes from z to sublevels, ex:

`subTree(2, 6)`

from z = 2 to z = 3

from z = 3 to z = 4

from z = 4 to z = 5

from z = 5 to z = 6

z = 6 !

Now, how can i make it so that the call to the function returns an ordered vector of z ?

(in the example it would be: z[2,3,4,5,6])

Answer Source

This code returns a list [2,3,4,5,6]

```
def subTree(z,sublevels,a):
a.append(z)
if(z < sublevels):
return subTree(z+1,sublevels,a)
else:
return(a)
a=[]
subTree(2,6,a)
```