class TreeToListByLevel {

  public List<BinaryNode> transform(BinaryNode tree) {
    if (tree == null) {
      return Collections.EMPTY_LIST;
    }

    List<BinaryNode> nodesByLevel = new LinkedList<BinaryNode>();
    Queue<BinaryNode> stack = new LinkedList<BinaryNode>();
    stack.add(tree);
    while (!stack.isEmpty()) {
      BinaryNode node = stack.remove();
      nodesByLevel.add(node);
      if (node.getLeft() != null) {
        stack.add(node.getLeft());
      }
      if (node.getRight() != null) {
        stack.add(node.getRight());
      }
    }
    return nodesByLevel;
  }

}