Iterator<T> () { return new Iterator<T>() { Tree<T> curr = Tree.this; @Override public void remove() {} @Override public boolean hasNext() { return curr.parent != null; } @Override public T next() { if (!hasNext()) { throw new NoSuchElementException(); } final T res = curr.elem; // IDEA inspection: Local variable 'res' is redundant (IDEA-163786) curr = curr.parent; return res; } }; }