# 103 Binary Tree Zigzag Level Order Traversal

## 103. Binary Tree Zigzag Level Order Traversal

## 1. Question

Given a binary tree, return thezigzag level ordertraversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).

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

```
    3
   / \
  9  20
    /  \
   15   7
```

return its zigzag level order traversal as:

```
[
  [3],
  [20,9],
  [15,7]
]
```

## 2. Implementation

**(1) BFS**

```java
class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) {
            return res;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        boolean reversed = false;
        List<Integer> level = null;
        int depth = 0;

        while (!queue.isEmpty()) {
            int size = queue.size();
            level = new ArrayList<>();

            for (int i = 0; i < size; i++) {
                TreeNode curNode = queue.remove();

                if (depth % 2 == 0) {
                    level.add(curNode.val);
                }
                else {
                    level.add(0, curNode.val);
                }

                if (curNode.left != null) {
                    queue.add(curNode.left);
                }

                if (curNode.right != null) {
                    queue.add(curNode.right);
                }
            }
            res.add(level);
            ++depth;
        }
        return res;
    }
}
```

**(2) DFS**

```java
class Solution {
    public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        getZigzagLevelOrderByDFS(root, 0, res);
        return res;
    }

    public void getZigzagLevelOrderByDFS(TreeNode node, int depth, List<List<Integer>> res) {
        if (node == null) {
            return;
        }

        if (res.size() == depth) {
            res.add(new ArrayList<>());
        }

        if (depth % 2 == 0) {
            res.get(depth).add(node.val);
        }
        else {
            res.get(depth).add(0, node.val);
        }

        getZigzagLevelOrderByDFS(node.left, depth + 1, res);
        getZigzagLevelOrderByDFS(node.right, depth + 1, res);
    }
}
```

## 3. Time & Space Complexity

BFS: 时间复杂度:O(n) , 空间复杂度:O(n)

DFS: 时间复杂度:O(n), 空间复杂度: O(n)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://protegejj.gitbook.io/my-algorithm-summary/data-structure/tree/103-binary-tree-zigzag-level-order-traversal.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
