title: Finding the number of nodes at each level during bfs
body: <p>You have been given a Tree consisting of N nodes. A tree is a fully-connected graph consisting of N nodes and N−1 edges. The nodes in this tree are indexed from 1 to N. Consider node indexed 1 to be the root node of this tree. The root node lies at level one in the tree. You shall be given the tree and a single integer x . You need to find out the number of nodes lying on level x.</p>
  
  <p>Input Format</p>
  
  <p>The first line consists of a single integer N denoting the number of nodes in the tree. Each of the next n−1 lines consists of 2 integers a andb denoting an undirected edge between node a and node b. The next line consists of a single integer x.</p>
  
  <p>Output Format</p>
  
  <p>You need to print a single integers denoting the number of nodes on level x.</p>
  
  <hr>
  
  <p>below is the code snippet i wrote and its not correct. Please help me find the error.</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.LinkedList;
  import java.util.Scanner;
  
  public class LevelNodes {
      public static ArrayList[] adj;
      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          int N = sc.nextInt();
          boolean[] visites = new boolean[N];
          ArrayList[] adj = new ArrayList[N];
          for(int j=0;j&lt;N;j++){
              adj[j] = new ArrayList();
          }
          for(int i = 0;i &lt; (N-1) ;i++){
              int a = sc.nextInt();
              int b = sc.nextInt();
              adj[a-1].add(b);
              adj[b-1].add(a);
          }
          int level = sc.nextInt();
          int counter = 0;
          LinkedList list = new LinkedList();
          list.add(1);
          counter++;
          while(!list.isEmpty()){
              int n = (Integer) list.poll();
              for(int x = 0; x&lt; adj[n-1].size();x++){
                  list.add(adj[n-1].get(x));
              }
              counter++;
              if(counter == level){
                  System.out.println(list.size());
                  break;
              }
  
          }
      }
  
  }
  </code></pre>