@@ -0,0 +1,1786 @@
+package leetcode.binary_tree_level_order_traversal;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.LinkedList;%0Aimport java.util.Queue;%0Aimport java.util.Random;%0A%0Aimport common.TreeNode;%0A%0Apublic class Solution %7B%0A%09public ArrayList%3CArrayList%3CInteger%3E%3E levelOrder(TreeNode root) %7B%0A%09%09Queue%3CMyNode%3E queue = new LinkedList%3CMyNode%3E();%0A%09%09ArrayList%3CArrayList%3CInteger%3E%3E ans = new ArrayList%3CArrayList%3CInteger%3E%3E();%0A%09%09if(root == null) return ans;%0A%09%09queue.offer(new MyNode(root, 0));%0A%09%09while(!queue.isEmpty())%7B%0A%09%09%09MyNode myNode = queue.remove();%0A%09%09%09TreeNode tn = myNode.node;%0A%09%09%09ArrayList%3CInteger%3E lvl = null;%0A%09%09%09if(ans.size() %3C= myNode.level)%7B%0A%09%09%09%09lvl = new ArrayList%3CInteger%3E();%0A%09%09%09%09ans.add(lvl);%0A%09%09%09%7Delse%7B%0A%09%09%09%09lvl = ans.get(myNode.level);%0A%09%09%09%7D%0A%09%09%09lvl.add(tn.val);%0A%09%09%09if(tn.left != null) queue.offer(new MyNode(tn.left, myNode.level + 1));%0A%09%09%09if(tn.right != null) queue.offer(new MyNode(tn.right, myNode.level + 1));%0A%09%09%7D%0A%09%09return ans;%0A%09%7D%0A%09public static class MyNode%7B%0A%09%09TreeNode node;%0A%09%09int level;%0A%09%09public MyNode(TreeNode node, int level)%7B%0A%09%09%09this.node = node;%0A%09%09%09this.level = level;%0A%09%09%7D%0A%09%7D%0A%0A%09public static void main(String%5B%5D args)%7B%0A%09%09final int N = 15;%0A%09%09final int R = 5;%0A%09%09ArrayList%3CTreeNode%3E nodes = new ArrayList%3CTreeNode%3E();%0A%09%09for(int i = 0; i %3C N; i++)%7B%0A%09%09%09nodes.add(new TreeNode(i));%0A%09%09%7D%0A%09%09Random r = new Random();%0A%09%09for(int i = 0; i %3C N; i++)%7B%0A%09%09%09if(i * 2 + 1 %3C N)%7B%0A%09%09%09%09nodes.get(i).left = nodes.get(i * 2 + 1);%0A%09%09%09%7D%0A%09%09%09if(i * 2 + 2 %3C N)%7B%0A%09%09%09%09nodes.get(i).right = nodes.get(i * 2 + 2);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09for(int i = 0; i %3C R; i++)%7B%0A%09%09%09int ran = r.nextInt(N);%0A%09%09%09System.err.println(%22ran:%22 + ran);%0A%09%09%09nodes.get(ran).left = null;%0A%09%09%7D%0A%09%09%0A%09%09for(ArrayList%3CInteger%3E row : new Solution().levelOrder(nodes.get(0)))%7B%0A%09%09%09for(int x : row)%7B%0A%09%09%09%09System.err.print(x + %22, %22);%0A%09%09%09%7D%0A%09%09%09System.err.println(%22%22);%0A%09%09%7D%0A%09%7D%0A%7D%0A

Add java solution for 102. Binary Tree Level Order Traversal