Type like pro

Index

Print nodes of a given level

Print nodes of a given level

Problem

Given a binary tree and a level, print all the nodes of the corresponding level.

Solution

We call a recursive function on the root of the binary tree by passing the given level. We do a preorder traversal of the binary tree, every time we call the recursive function on any node's children, we decrease the level value by one. When level value is zero, that means we reached at the correct level. Then we print the node's value and stop proceeding to further depths.

Code

public class PrintNodeOfSameLevel
{
 public static void main(String[] args)
 {
  Node a = new Node(1);
  Node b = new Node(2);
  Node c = new Node(3);
  Node d = new Node(4);
  Node e = new Node(5);
  Node f = new Node(6);
  Node g = new Node(7);
  Node h = new Node(8);
  a.left = b;
  a.right = c;
  b.left = d;
  c.left = e;
  c.right = f;
  f.left = g;
  f.right = h;

  printLevelofDepth(a, 5);
 }

 private static void printLevelofDepth(Node a, int depth)
 {
  if (a == null)
   return;
  if (depth == 1)
  {
   System.out.println(a.value);
   return;
  }
  printLevelofDepth(a.left, depth - 1);
  printLevelofDepth(a.right, depth - 1);
 }

 static class Node
 {
  Node left;
  Node right;
  int value;

  public Node(int value)
  {
   this.value = value;
  }
 }
}