Accepted python code with DFS (1073ms):

```
def lexicalOrder(self, n):
def dfs(k, res):
if k <= n:
res.append(k)
t = 10*k
if t <= n:
for i in range(10):
dfs(t + i, res)
res = []
for i in range(1, 10):
dfs(i, res)
return res
```

Interestingly, with only one modification to the above code, the following code gets Memory Limit Exceeded.

```
def lexicalOrder(self, n):
def dfs(k):
if k <= n:
res.append(k)
t = 10*k
if t <= n:
for i in range(10):
dfs(t + i)
res = []
for i in range(1, 10):
dfs(i)
return res
```

The only difference between these two is that, in the latter one, we do not pass the list `res`

as an argument to the function `dfs`

. Can anybody explain this phenomenon? Thanks!