2 Oct 2013

Print nodes at level K in a Binary Tree

Problem Statement:
   Print all the nodes at a given level in a Binary Tree

Solution:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
     //---- Print all nodes at given level---------
 public void printGivenLevel(Node root, int level) {
  
  if(root == null)
   return;
  
  if(level == 1)
   System.out.print(root.data + "\t");

   printGivenLevel(root.left, level-1);
   printGivenLevel(root.right, level-1);
 }

No comments:

Post a Comment