Last active
October 22, 2015 01:05
-
-
Save dmuneras/b647f875fc953a1d6488 to your computer and use it in GitHub Desktop.
tree_by_levels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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