/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem66;

import com.github.pedrovgs.binarytree.BinaryNode;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * Given a binary tree, design an algorithm which creates a linked list of all the nodes at
 * each depth (e.g., if you have a tree with depth D, you'll have D linked lists).
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class TreeToListByLevel {

}

@@ -1062,10 +1062,754 @@
 evel %7B%0A%0A
+  /**%0A   * Iterative algorithm to resolve this problem. Binary tree traversal by level implemented with a%0A   * complexity order in time and space terms equivalent to O(N).%0A   */%0A  public List%3CBinaryNode%3E transform(BinaryNode tree) %7B%0A    if (tree == null) %7B%0A      return Collections.EMPTY_LIST;%0A    %7D%0A%0A    List%3CBinaryNode%3E nodesByLevel = new LinkedList%3CBinaryNode%3E();%0A    Queue%3CBinaryNode%3E stack = new LinkedList%3CBinaryNode%3E();%0A    stack.add(tree);%0A    while (!stack.isEmpty()) %7B%0A      BinaryNode node = stack.remove();%0A      nodesByLevel.add(node);%0A      if (node.getLeft() != null) %7B%0A        stack.add(node.getLeft());%0A      %7D%0A      if (node.getRight() != null) %7B%0A        stack.add(node.getRight());%0A      %7D%0A    %7D%0A    return nodesByLevel;%0A  %7D%0A
 %7D%0A

Add solution to problem 66