@@ -0,0 +1,1632 @@
+/* 4.4 Given a binary search tree, design an algorithm which creates a linked%0A * list of all the nodes at each depth (i.e., if you have a tree with depth D,%0A * you will have D linked lists).%0A * */%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Queue;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.Iterator;%0A%0Apublic class P0404 %7B%0A   private class Node %7B%0A      int value;%0A      Node left, right;%0A%0A      public Node(int value) %7B%0A         this.value = value;%0A      %7D%0A   %7D%0A%0A   public void levelOrder(Node root, int level, int count, %0A      List%3CNode%3E list) %7B%0A%0A      if (root == null) return;%0A%0A      if (count == level) %7B%0A         list.add(root);%0A         return;%0A      %7D%0A%0A      if (count %3C level) %7B%0A         levelOrder(root.left, level, count + 1, list);%0A         levelOrder(root.right, level, count + 1, list);%0A      %7D%0A   %7D%0A%0A   public Node buildTree() %7B%0A      Node root = new Node(1);%0A      root.left = new Node(2);%0A      root.right = new Node(3);%0A      root.left.left = new Node(4);%0A      root.left.right = new Node(5);%0A      root.right.left = new Node(6);%0A      root.left.left.left = new Node(7);%0A      //root.left.left.left.left = new Node(8);%0A      return root;%0A   %7D%0A%0A   public static void main(String%5B%5D args) %7B%0A      P0404 p0404 = new P0404();%0A      Node root = p0404.buildTree();%0A%0A      for (int i = 1; i %3C= 4; i++) %7B%0A         List%3CNode%3E list = new LinkedList%3CNode%3E();%0A         p0404.levelOrder(root, i, 1, list);%0A         Iterator%3CNode%3E iter = list.iterator();%0A         while (iter.hasNext()) %7B%0A            System.out.print(iter.next().value + %22 %22);%0A         %7D%0A         System.out.println();%0A      %7D%0A   %7D%0A%7D%0A

implement traversal at a given level