Check out example codes for "C# walk down a tree and back". It will help you in understanding the concepts better.
Code Example 1
public static IEnumerable<T> Traverse<T>(T item, Func<T, IEnumerable<T>> childSelector)
{
var stack = new Stack<T>();
stack.Push(item);
while (stack.Any())
{
var next = stack.Pop();
yield return next;
foreach (var child in childSelector(next))
stack.Push(child);
}
}
Learn ReactJs, React Native from akashmittal.com