title: Building a tree with parent child relationship using c# recursively
body: <p>I have a list which I want to convert into tree structure. How can I convert this into tree structure? </p>
  
  <p>I have looked into <a href="https://stackoverflow.com/questions/15867478/build-tree-type-list-by-recursively-checking-parent-child-relationship-c-sharp">Build tree type list by recursively checking parent-child relationship C#</a> thread but since key in my class is string, I cannot use that solution. Please help </p>
  
  <pre><code>internal class Program
  {
          private static void Main(string[] args)
          {
              List&lt;node&gt; nodeList = new List&lt;node&gt;();
              node n = new node("A", "A1", null, 1); nodeList.Add(n);
              n = new node("B", "A2", "A1", 2); nodeList.Add(n);
              n = new node("C", "A3", "A1", 2); nodeList.Add(n);
              n = new node("D", "A4", "A1", 2); nodeList.Add(n);
  
              n = new node("E", "A5", "A2", 3); nodeList.Add(n);
              n = new node("F", "A6", "A5", 4); nodeList.Add(n);
              n = new node("G", "A7", "A3", 3); nodeList.Add(n);
              n = new node("H", "A8", "A4", 3); nodeList.Add(n);
              n = new node("I", "A9", "A4", 3); nodeList.Add(n);
              n = new node("J", "A10", "A4", 3); nodeList.Add(n);
              n = new node("K", "A11", "A10", 4); nodeList.Add(n);
              n = new node("L", "A12", "A10", 4); nodeList.Add(n);
              n = new node("M", "A13", "A12", 5); nodeList.Add(n);
              n = new node("N", "A14", "A12", 5); nodeList.Add(n);
              n = new node("O", "A15", "A10", 4); nodeList.Add(n);
  
              n = new node("P", "A16", null, 1); nodeList.Add(n);
              n = new node("Q", "A17", "A16", 2); nodeList.Add(n);
          }
  }
  
  public class node
  {
          public string name { get; set; }
          public string key { get; set; }
          public string parentKey { get; set; } 
          public int level { get; set; }
  
          public List&lt;node&gt; Children { get; set; }
  
          public node(string Name, string Key, string PK, int Level)
          {
              name = Name;
              key = Key;
              parentKey = PK;
              level = Level;
          }
  }
  </code></pre>