Skip to content

Instantly share code, notes, and snippets.

@dmuneras
Last active October 22, 2015 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmuneras/b647f875fc953a1d6488 to your computer and use it in GitHub Desktop.
Save dmuneras/b647f875fc953a1d6488 to your computer and use it in GitHub Desktop.
tree_by_levels
def tree_by_levels(node)
return [] unless node
array = [node]
result = []
i=0
while (i+1) <= array.size
array << array[i].left if array[i].left
array << array[i].right if array[i].right
result << array[i].value
i += 1
end
result
end
# toco pedirle ayuda al cuñado
def tree_by_levels(node)
return [] unless node
array = [node]
i=0
while (i+1) <= array.size && node
array << array[i].left if array[i].left
array << array[i].right if array[i].right
i += 1
end
array.map{|node| node.value}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment