The solution uses recursion to clean data on the passed node first and then recursively walks through all children. There are a couple of important moments to note. First, don’t forget to check for null on the passed parameter. Another approach would be throwing an ArgumentException if root is null.

Note that String.Empty is used instead of double quotes. Many people fail to discuss the difference between those two.  The main difference is that String.Empty will not create any objects, while “” will create a new instance of string object in memory. So, always use String.Empty for the ultimately in memory efficiency.

public void Clean(Node root)
if (root != null)
root.Data = string.Empty;
foreach (Node child in root.Children) Clean(child);

Be prepare to answer following questions:

What is the execution time for this algorithm in bigO notation?How does recursion work internally?
Is this a BFS or DFS algorithm?

Answers and Comments