# 103. Binary Tree Zigzag Level Order Traversal!

{% hint style="info" %}
reverse the order globally not locally
{% endhint %}

```java
class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> result = new LinkedList<>();
        if(root==null) return result;
        queue.add(root);
        // true: add to list using add(addlast)
        // false: add to list uising addfirst
        boolean flag = true;
        while (!queue.isEmpty()){
            int size = queue.size();
            // to use linkedlist addFirst, and addLast method
            LinkedList<Integer> list = new LinkedList<>();
            while(size-->0){
                root = queue.poll();
                if(flag){
                    // add from left to right
                    list.add(root.val); 
                }else{
                    // add from right to left
                    list.addFirst(root.val);
                }
                
                if(root.left!=null) queue.offer(root.left);
                if(root.right!=null) queue.offer(root.right);
            }
            flag = !flag;
            result.add(list);
        }
        return result;
    }
}
```
