public static IEnumerator<DecisionNode> DepthFirst(DecisionNode tree) { if (tree == null) yield break; var stack = new Stack<DecisionNode>(new[] { tree }); while (stack.Count != 0) { DecisionNode current = stack.Pop(); yield return current; if (current.Branches != null) for (int i = current.Branches.Count - 1; i >= 0; i--) stack.Push(current.Branches[i]); } }