title: Breadth-first traversal
body: <p>I was trying to solve one interview question, but for that I have to travel the binary tree level by level. I have designed BinaryNode with having below variable </p>
  
  <pre><code>private object data;
  private BinaryNode left;
  private BinaryNode right;
  </code></pre>
  
  <p>Could someone please help to write the BreadthFirstSearch method inside my BinarySearchTree class?</p>
  
  <p>Update: Thanks everyone for your inputs. So this was the interview question.
  "Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if you have a tree with depth D, you’ll have D linked lists)".</p>
  
  <p>Here is my Method, let me know your expert comment.</p>
  
  <pre><code>public List&lt;LinkedList&lt;BNode&gt;&gt; FindLevelLinkList(BNode root)
      {
          Queue&lt;BNode&gt; q = new Queue&lt;BNode&gt;();
          // List of all nodes starting from root.
          List&lt;BNode&gt; list = new List&lt;BNode&gt;();
          q.Enqueue(root);
          while (q.Count &gt; 0)
          {
              BNode current = q.Dequeue();
              if (current == null)
                  continue;
              q.Enqueue(current.Left);
              q.Enqueue(current.Right);
              list.Add(current);
          }
  
          // Add tree nodes of same depth into individual LinkedList. Then add all LinkedList into a List
          LinkedList&lt;BNode&gt; LL = new LinkedList&lt;BNode&gt;();
          List&lt;LinkedList&lt;BNode&gt;&gt; result = new List&lt;LinkedList&lt;BNode&gt;&gt;();
          LL.AddLast(root);
          int currentDepth = 0;
          foreach (BNode node in list)
          {
             if (node != root)
              {
                  if (node.Depth == currentDepth)
                  {
                      LL.AddLast(node);
                  }
                  else
                  {
                      result.Add(LL);
                      LL = new LinkedList&lt;BNode&gt;();
                      LL.AddLast(node);
                      currentDepth++;
                  }
              }
          }
  
          // Add the last linkedlist
          result.Add(LL);
          return result;
      }
  </code></pre>