Print left view of the tree Left view of a Binary Tree is set of nodes visible when tree is visited from left side. Left view of following tree is 12, 10, 25.

       12

    /     \
 10       30
        /    \
      25      40 


Looking for code review, optimizations and best practices. Also verifying space complexity is O(n) and not O(logn) where n is number of nodes in tree.

public class LeftView&lt;T&gt; {

    private TreeNode&lt;T&gt; root;

    /**
     * Takes in a BFS representation of a tree, and converts it into a tree.
     * here the left and right children of nodes are the (2*i + 1) and (2*i + 2)nd
     * positions respectively.
     * 
     * @param items The items to be node values.
     */
    public LeftView(List&lt;? extends T&gt; items) {
        create(items);
    }

    private void create (List&lt;? extends T&gt; items) {
        root = new TreeNode&lt;T&gt;(null, items.get(0), null);

        final Queue&lt;TreeNode&lt;T&gt;&gt; queue = new LinkedList&lt;TreeNode&lt;T&gt;&gt;();
        queue.add(root);

        final int half = items.size() / 2;

        for (int i = 0; i &lt; half; i++) {
            if (items.get(i) != null) {
                final TreeNode&lt;T&gt; current = queue.poll();                
                final int left = 2 * i + 1;
                final int right = 2 * i + 2;

                if (items.get(left) != null) {
                    current.left = new TreeNode&lt;T&gt;(null, items.get(left), null);
                    queue.add(current.left);
                }
                if (right &lt; items.size() &amp;&amp; items.get(right) != null) {
                    current.right = new TreeNode&lt;T&gt;(null, items.get(right), null);
                    queue.add(current.right);
                }
            }
        }
    }

    private static class TreeNode&lt;E&gt; {
        TreeNode&lt;E&gt; left;
        E item;
        TreeNode&lt;E&gt; right;

        TreeNode(TreeNode&lt;E&gt; left, E item, TreeNode&lt;E&gt; right) {
            this.left = left;
            this.item = item;
            this.right = right;
        }
    }


    /**
     * Returns the left view of the binary tree
     * 
     * @return  the left view of binary tree.
     */
    public List&lt;T&gt; leftView() {
        if (root == null) { throw new NoSuchElementException("The root is null"); }

        final List&lt;T&gt; leftView = new ArrayList&lt;T&gt;();
        computeLeftView(root, leftView, 1);
        return leftView;
    }

    private void computeLeftView(TreeNode&lt;T&gt; node, List&lt;T&gt; leftView, int currentDepth) {
        if (node != null) {
            if (currentDepth &gt; leftView.size()) {
                leftView.add(node.item);
            }
            computeLeftView(node.left, leftView, currentDepth + 1);
            computeLeftView(node.right, leftView, currentDepth + 1);
        }
    }


    public static void main(String[] args) {
        final LeftView&lt;Integer&gt; leftView1 = new LeftView&lt;Integer&gt;(Arrays.asList(1, 2, 3, 4, 5, 6, 7));
        assertEquals(Arrays.asList(1, 2, 4)  , leftView1.leftView());

        final LeftView&lt;Integer&gt; leftView2 = new LeftView&lt;Integer&gt;(Arrays.asList(1, 2, 3, 4, null, 6, 7));
        assertEquals(Arrays.asList(1, 2, 4)  , leftView2.leftView());

        final LeftView&lt;Integer&gt; leftView3 = new LeftView&lt;Integer&gt;(Arrays.asList(1, 2, 3, null, 5, 6, 7));
        assertEquals(Arrays.asList(1, 2, 5)  , leftView3.leftView());

        final LeftView&lt;Integer&gt; leftView4 = new LeftView&lt;Integer&gt;(Arrays.asList(1, 2, 3, null, null, 6, 7));
        assertEquals(Arrays.asList(1, 2, 6)  , leftView4.leftView());
    }
}
Although the algorithms seems fine, there are issues with the choosen abstractions.

LeftView is a factory of lists, which are left views of an inner tree. LeftView is not a data structure itself, but a factory of it, configured by a list, that are converted to a tree -- too much responsability. Keeping in mind @abuzittin gillifirca advice, a good practise is to refactor factory methods in static, public, methods.

public final class BinaryTrees {
    public static List&lt;T&gt; createLeftViewList(BinaryTree&lt;T&gt; tree) { ... }
}


In your code, LeftView had his own tree representation. To better convey the idea of an algorithm, consider start with simple interfaces, followed by the algorithm itself. That way you get better readability and guarantee further reuse in real code.

public interface BinaryTree&lt;T&gt; {
    BinaryTree&lt;T&gt; left();
    BinaryTree&lt;T&gt; right();
    T element();
}


And than we fill it with runnable code.

public class BinaryTree&lt;T&gt; {
    public BinaryTree&lt;T&gt; left() { return left; }
    public BinaryTree&lt;T&gt; right() { return right; }
    public T element() { return element; }

    private BinaryTree&lt;T&gt; left, right;
    private T element;

    public BinaryTree(List&lt;? extends T&gt; bfsRepresentation) {
        final List&lt;? extends T&gt; items;
        items = new ArrayList&lt;&gt;( bfsRepresentation );
        create( items );
    }
    ...
}


The create method assumes that items is always random access list. Well, it is if we consider only the test methods. If it gets called with a very large LinkedList, you will get quadratic performance.

Consider copying the list in an java.util.ArrayList. An array, T[], should do the trick too, and its preferrable.

Last piece of advice, consider parameterized unit tests in JUnit. There's a good example how to use it in their github wiki.