Skip to content

Instantly share code, notes, and snippets.

@jfelipe
Created October 21, 2015 22:58
Show Gist options
  • Save jfelipe/3a71b71bb752f9e8a129 to your computer and use it in GitHub Desktop.
Save jfelipe/3a71b71bb752f9e8a129 to your computer and use it in GitHub Desktop.
Code wars "Sort binary tree by levels"
# return the array containing the tree levels, from root to last level.
def tree_by_levels(node)
return [] unless node
result = []
q = [node]
until q.empty?
node = q.shift
q << node.left if node.left
q << node.right if node.right
result << node.value
end
result
end
@pedroskiplgo
Copy link

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment