package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 102. Binary Tree Level Order Traversal

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its level order traversal as:

[
  [3],
  [9,20],
  [15,7]
]
*/
public class _102 {

	public List<List<Integer>> levelOrder(TreeNode root) {
		List<List<Integer>> result = new ArrayList<>();
		if (root == null) {
			return result;
		}
		Queue<TreeNode> q = new LinkedList();
		q.offer(root);
		while (!q.isEmpty()) {
			List<Integer> thisLevel = new ArrayList();
			int qSize = q.size();
			for (int i = 0; i < qSize; i++) {
				TreeNode curr = q.poll();
				thisLevel.add(curr.val);
				if (curr.left != null) {
					q.offer(curr.left);
				}
				if (curr.right != null) {
					q.offer(curr.right);
				}
			}
			result.add(thisLevel);
		}
		return result;
	}

}

@@ -572,17 +572,55 @@
 02 %7B%0D%0A%0D%0A
-%09
+  public static class Solution1 %7B%0D%0A    
 public L
@@ -667,18 +667,22 @@
 oot) %7B%0D%0A
-%09%09
+      
 List%3CLis
@@ -722,18 +722,22 @@
 t%3C%3E();%0D%0A
-%09%09
+      
 if (root
@@ -749,19 +749,24 @@
 ull) %7B%0D%0A
-%09%09%09
+        
 return r
@@ -773,23 +773,31 @@
 esult;%0D%0A
-%09%09%7D%0D%0A%09%09
+      %7D%0D%0A      
 Queue%3CTr
@@ -827,18 +827,22 @@
 ist();%0D%0A
-%09%09
+      
 q.offer(
@@ -849,18 +849,22 @@
 root);%0D%0A
-%09%09
+      
 while (!
@@ -879,19 +879,24 @@
 y()) %7B%0D%0A
-%09%09%09
+        
 List%3CInt
@@ -931,19 +931,24 @@
 ist();%0D%0A
-%09%09%09
+        
 int qSiz
@@ -962,19 +962,24 @@
 ize();%0D%0A
-%09%09%09
+        
 for (int
@@ -1005,20 +1005,26 @@
 i++) %7B%0D%0A
-%09%09%09%09
+          
 TreeNode
@@ -1042,20 +1042,26 @@
 oll();%0D%0A
-%09%09%09%09
+          
 thisLeve
@@ -1078,20 +1078,26 @@
 .val);%0D%0A
-%09%09%09%09
+          
 if (curr
@@ -1106,37 +1106,44 @@
 eft != null) %7B%0D%0A
-%09%09%09%09%09
+            
 q.offer(curr.lef
@@ -1151,19 +1151,31 @@
 );%0D%0A
-%09%09%09%09%7D%0D%0A%09%09%09%09
+          %7D%0D%0A          
 if (
@@ -1201,13 +1201,20 @@
  %7B%0D%0A
-%09%09%09%09%09
+            
 q.of
@@ -1235,24 +1235,40 @@
 );%0D%0A
-%09%09%09%09%7D%0D%0A%09%09%09%7D%0D%0A%09%09%09
+          %7D%0D%0A        %7D%0D%0A        
 resu
@@ -1291,15 +1291,23 @@
 );%0D%0A
-%09%09%7D%0D%0A%09%09
+      %7D%0D%0A      
 retu
@@ -1322,12 +1322,18 @@
 t;%0D%0A
-%09%7D%0D%0A
+    %7D%0D%0A  %7D
 %0D%0A%7D%0D

refactor 102