public IEnumerator <ITreeItem> TraverseDepthFirst() { yield return(Root); var stack = new Stack <IEnumerator <ITreeItem> >(); stack.Push(Root.GetEnumerator()); while (stack.Count > 0) { IEnumerator <ITreeItem> en = stack.Peek(); if (en.MoveNext()) { ITreeItem n = en.Current; yield return(n); if (n.CanSubdivide) { en = n.GetEnumerator(); stack.Push(en); } } else { stack.Pop(); } } }