Skip to content

Instantly share code, notes, and snippets.

@tamlt2704
Created February 8, 2018 00:54
Show Gist options
  • Save tamlt2704/69cd6c73fe97d4903127987917e37e44 to your computer and use it in GitHub Desktop.
Save tamlt2704/69cd6c73fe97d4903127987917e37e44 to your computer and use it in GitHub Desktop.
g = {1: [2, 3], 2: [4, 5], 3: [6, 7], 6: [7], 7: [9]}
arr = [1]
visited = [1]
while arr:
n = arr[0]
if n not in g.keys():
print arr
arr.pop(0)
else:
allChildVisited = True
for x in g[n]:
if x not in visited:
arr.insert(0, x)
visited.append(x)
allChildVisited = False
break
if allChildVisited:
arr.pop(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment