title: C# binary tree data structure with static root node
body: <p>I tried creating a data structure for a static binary tree with a static root value and 2 child nodes. I am trying to make it dynamic for any number of child values. How can I do that with a static root node. How can I implement if I take myArray = {3,11,8,18,21,36,1}. Any simple code with out complex code changes would help.</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          TreeNode rootNode = new TreeNode();
          rootNode.value = 9;
  
          int[] myArray = { 3, 11 };
  
          for (int i = 0; i &lt; myArray.Length; i++)
          {
              if (myArray[i] &gt; rootNode.value)
              {
                  //add to right node
                  TreeNode right = new TreeNode();
                  right.value = myArray[i];
                  rootNode.rightNode = right;
  
              }
              else
              {
                  //add to left node
                  TreeNode left = new TreeNode();
                  left.value = myArray[i];
                  rootNode.leftNode = left;
              }
          }
      }
  }
  
  class TreeNode
  {
      public int value { get; set; }
      public TreeNode leftNode { get; set; }
      public TreeNode rightNode { get; set; }
  
  }
  </code></pre>