You are given a binary tree:

```python
class Node:
    def __init__(self, L, R, n):
        self.left = L
        self.right = R
        self.value = n
```

Your task is to return the list with elements from tree sorted by levels, which means the root element goes first, then root children (from left to right) are second and third, and so on.
```if:ruby
Return empty array if root is `nil`.
```
```if:haskell
Return empty list if root is `Nothing`.
```
```if:python
Return empty list if root is `None`.
```
```if:csharp
Return empty list if root is 'null'.
```
```if:java
Return empty list is root is 'null'.
```

Example 1 - following tree:

                     2
                8        9
              1  3     4   5

Should return following list:

    [2,8,9,1,3,4,5]

Example 2 - following tree:

                     1
                8        4
                  3        5
                             7
Should return following list:

    [1,8,4,3,5,7]

Difficulty: interview

Starter code:

def tree_by_levels(node):
	

Solutions:
from collections import deque


def tree_by_levels(node):
    if not node:
        return []
    res, queue = [], deque([node,])
    while queue:
        n = queue.popleft()
        res.append(n.value)
        if n.left is not None:
            queue.append(n.left)
        if n.right is not None:
            queue.append(n.right)
    return res

def tree_by_levels(node):
    p, q = [], [node]
    while q:
        v = q.pop(0)
        if v is not None:
            p.append(v.value)
            q += [v.left,v.right]
    return p if not node is None else []

def tree_by_levels(tree):
    queue = [tree]
    values = []

    while queue:
        node = queue.pop(0)
        if node:
            queue += [node.left, node.right]
            values.append(node.value)
            
    return values

# Use a queue. Add root node, then loop:
# get first queue element, add all its children in the queue, left to right
# and add the current node's value in the result

def tree_by_levels(node):
    queue = []
    result = []
    if node == None:
        return result
    queue.append(node)
    while len(queue) > 0:
        n = queue.pop(0)
        if n.left != None:
            queue.append(n.left)
        if n.right != None:
            queue.append(n.right)
        result.append(n.value)
    return result

def tree_by_levels(node):
    r = []
    nodes = [node]
    while nodes:
        r += [n.value for n in nodes if n]
        nodes = [e for n in [(n.left, n.right) for n in nodes if n] for e in n if e]
        
    return r

from collections import deque

def tree_by_levels(node):
    if not node: return []
    queue = deque([node])
    level_order = []
    while queue:
        cur_node = queue.popleft()
        level_order.append(cur_node.value)
        if cur_node.left: queue.append(cur_node.left)
        if cur_node.right: queue.append(cur_node.right)
    return level_order

def tree_by_levels(node):
  return [v for v, _, _ in sorted(extract_nodes(node), key = lambda x: (x[1], x[2]))]

def extract_nodes(node, lvl=0, directions=[]):
  if node:
    return [(node.value, lvl, directions)] + extract_nodes(node.left, lvl+1, directions + [-1]) + extract_nodes(node.right, lvl+1, directions + [1])
  return []

from itertools import chain
def tree_by_levels(node):
    tree = [node]
    temp = [t.value for t in tree if t]
    while tree:
        tree = list(chain(*[[t.left, t.right] for t in tree if t]))
        temp += [t.value for t in tree if t]
    return temp

def tree_by_levels(node):
    levels = {}
    def sweep(subnode, level):
        if subnode != None:
            levels[level] = levels.get(level, []) + [subnode.value]
            sweep(subnode.left,  level + 1)
            sweep(subnode.right, level + 1)
    sweep(node, 0)
    return sum((levels[k] for k in sorted(levels)), [])


from collections import deque


def tree_by_levels(node):
    q, result = deque(), []
    if node is not None:
        q.append(node)
    while len(q):
        node = q.popleft()
        result.append(node.value)
        q.extend(i for i in (node.left, node.right) if i is not None)
    return result