Skip to content

Instantly share code, notes, and snippets.


ssartell/10.cs Secret

Created May 15, 2019 17:39
What would you like to do?
/* LINQ Puzzle #10 *******************************************************
Summary: Assume we have a generic class that forms a tree like the Node class below.
Given an IEnumerable of Nodes, write an extension method that flattens all the nodes
of the tree(s) into a single list of nodes. The method MUST capable of accepting a
generic class. The only assumption you can make is that it has some property that
contains an IEnumerable of children of the same type.
void Main()
var nodes = Enumerable.Range(0, 3).Select(x => new Node() {
Children = Enumerable.Range(0, 3).Select(y => new Node() {
Children = Enumerable.Range(0, 1).Select(z => new Node()).ToList()
// call your extension method here
nodes.FlattenTree(x => x.Children).Dump();
public class Node {
public Node()
Guid = Guid.NewGuid();
public Guid Guid { get; set; }
public IEnumerable<Node> Children { get; set; }
// write your extension method here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment