Fastest zigzag: 0 ms Beats 100.00% Analyze Complexity Memory 4.45 MB Beats 95.97%
ApproachRecursive approachComplexity

Time complexity:
0 ms Beats 100.00%


Space complexity:
4.45 MB Beats 95.97%

Code
# Approach
Recursive approach

# Complexity
- Time complexity:
0 ms Beats 100.00%

- Space complexity:
4.45 MB Beats 95.97%

# Code
```golang []
import "slices"

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func zigzagLevelOrder(root *TreeNode) [][]int {
    level, levelSlice, zigZagSlice := 0, [][]int{}, [][]int{}
    levelWiseOrder(root, &level, &levelSlice)

    streight := true
    for _, v := range levelSlice {
        if streight {
            streight = false
        } else {
            slices.Reverse(v)
            streight = true
        }
        zigZagSlice = append(zigZagSlice, v)
    }
    return zigZagSlice
}

func levelWiseOrder(root *TreeNode, level *int, levelSlice *[][]int) {
    if root == nil { return }

    if len(*levelSlice) < (*level) + 1 {
        *levelSlice = append(*levelSlice, []int{})
    }
    (*levelSlice)[*level] = append((*levelSlice)[*level], root.Val)

    *level += 1
    levelWiseOrder(root.Left, level, levelSlice)
    levelWiseOrder(root.Right, level, levelSlice)
    *level -= 1
}
```
['Go']