The solution is to use an additional data structure such as Stack to maintain a list of unprocessed nodes, keep adding children to the stack, and then the parent is processed. As you can see, any other collection can be used such as List or an Array, but you would need to do some additional work for adding and removing elements from the collection.
void Clean(Node root)
if (root != null)
Stack stack = new Stack();
while (stack.Count > 0)
Node current = stack.Pop();
current.Data = string.Empty;
foreach (Node children in current.Children)
You might expect next follow up questions:
What is Stack?How is Stack different from Queue?Would it make a difference if we use Queue in the example above?