package com.fishercoder.solutions;

import com.fishercoder.common.classes.TreeNode;

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

public class _103 {
    public static class Solution1 {
        public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
            Queue<TreeNode> q = new LinkedList();
            List<List<Integer>> levels = new ArrayList();
            if (root == null) {
                return levels;
            }
            q.offer(root);
            boolean forward = true;
            while (!q.isEmpty()) {
                int size = q.size();
                List<Integer> level = new ArrayList();
                for (int i = 0; i < size; i++) {
                    TreeNode curr = q.poll();
                    level.add(curr.val);
                    if (curr.left != null) {
                        q.offer(curr.left);
                    }
                    if (curr.right != null) {
                        q.offer(curr.right);
                    }
                }
                if (forward) {
                    forward = false;
                    levels.add(level);
                } else {
                    Collections.reverse(level);
                    levels.add(level);
                    forward = true;
                }
            }
            return levels;
        }
    }
}

@@ -367,16 +367,20 @@
 eNode%3E q
+ueue
  = new L
@@ -536,24 +536,28 @@
            q
+ueue
 .offer(root)
@@ -615,16 +615,20 @@
 hile (!q
+ueue
 .isEmpty
@@ -661,16 +661,20 @@
 size = q
+ueue
 .size();
@@ -815,16 +815,20 @@
 curr = q
+ueue
 .poll();
@@ -931,32 +931,36 @@
                q
+ueue
 .offer(curr.left
@@ -1055,16 +1055,20 @@
        q
+ueue
 .offer(c
@@ -1139,16 +1139,17 @@
     if (
+!
 forward)
@@ -1175,55 +1175,27 @@
     
-forward = false;%0A                    levels.add
+Collections.reverse
 (lev
@@ -1220,15 +1220,8 @@
    %7D
- else %7B
 %0A   
@@ -1237,132 +1237,62 @@
     
-    Collections.reverse(level);%0A                    levels.add(level);%0A                    forward = true;%0A                %7D
+forward = !forward;%0A                levels.add(level);
 %0A   

refactor 103