using System.Collections.Generic;
public static void Main()
var numbers = new Dictionary<int,int[]>
{3, new [] { 8, 9, 10 }},
{4, new [] { 11, 12, 13 }},
{5, new [] { 14, 15, 16 }},
{9, new [] { 17, 18, 19 }},
{13, new [] { 20, 21, 22 }}
Console.WriteLine(string.Join(", ", demo.GetList(numbers, 1)));
public IEnumerable<int> GetList(Dictionary<int,int[]> dict, int root)
return GetChildren(root);
IEnumerable<int> GetChildren(int n) =>
: new [] { n }.Concat(dict[n].SelectMany(x => GetChildren(x)));