Q:

Transform array of array from all first elements

I'm trying to convert to following array of array
[
  "master_type", # [0]
  [
    "type_1",    # [1][0]
    [
      "type_2",  # [1][1][0]
      [
        "type_3" # [1][1][1][0]
      ]
    ],
    [
      "type_3"   # [1][2][0]
    ]
  ],
  [
    "type_4",    # [2][0]
    [
      "type_2"   # [2][1][0]
    ]
  ],
  [
    "type_2",    # [3][0]
    "type_3"     # [3][1]
  ],
  "type_3"       # [3][1]
]

into something like
[
  ["master_type", "type_1", "type_2", "type_3"],
  ["master_type", "type_1", "type_3"],
  ["master_type", "type_4", "type_2"],
  ["master_type", "type_2", "type_3"],
  ["master_type", "type_3"]
]

Finally I would like to read this tree and collect all first elements when there are first elements then delete them before doing it again until my first array was empty. 
I can't fix it.  Do you have any ideas ?  
It's important to note that this example is with strings but I'll do it with more complex objects
Thank you in advance. 

A:

This implementation is recursive, but I doubt it will overflow.  If your trees are really deep, you may want to make it iterative instead. 
def flatten_tree(tree)
  parent, *subtree = tree

  return [[parent]] if subtree.empty? 

  subtree.flat_map do |element|
    flatten_tree(element).each { |children| children.unshift(parent) }
  end
end

The answer was updated according to sawa's comments.