public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) {
        ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
        if(root == null) return res;
        
        ArrayList<TreeNode> nodes = new ArrayList<TreeNode>();
        nodes.add(root);
        boolean fromLeft = false;
        
        while(!nodes.isEmpty()) {
            ArrayList<Integer> list = new ArrayList<Integer>();
            for(TreeNode node : nodes) {
                list.add(node.val);
            }
            res.add(list);
            
            ArrayList<TreeNode> newNodes = new ArrayList<TreeNode>();
            for(int i = nodes.size() - 1; i >= 0; i--) {
                TreeNode node = nodes.get(i);
                if(fromLeft) {
                    if(node.left != null) newNodes.add(node.left);
                    if(node.right != null) newNodes.add(node.right);
                } else {
                    if(node.right != null) newNodes.add(node.right);
                    if(node.left != null) newNodes.add(node.left);
                }
            }
            fromLeft = !fromLeft;
            
            nodes = newNodes;
        }
        
        return res;
    }