✔️ 100% Fastest Swift Solution, time: O(n), space: O(n).
\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     public var val: Int\n *     public var left: TreeNode?\n *     public var right
```\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     public var val: Int\n *     public var left: TreeNode?\n *     public var right: TreeNode?\n *     public init() { self.val = 0; self.left = nil; self.right = nil; }\n *     public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }\n *     public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {\n *         self.val = val\n *         self.left = left\n *         self.right = right\n *     }\n * }\n */\nclass Solution {\n    // - Complexity:\n    //   - time: O(n), where n is the number of nodes.\n    //   - space: O(n), where n is the number of nodes.\n\n    func zigzagLevelOrder(_ root: TreeNode?) -> [[Int]] {\n        var ans = [[Int]]()\n        dfs(root, level: 0, ans: &ans)\n        return ans\n    }\n\n\n    private func dfs(_ node: TreeNode?, level: Int, ans: inout [[Int]]) {\n        guard let node = node else { return }\n        if ans.count <= level { ans.append([Int]()) }\n\n        if level % 2 == 0 {\n            ans[level].append(node.val)\n        } else {\n            ans[level].insert(node.val, at: 0)\n        }\n\n        dfs(node.left, level: level + 1, ans: &ans)\n        dfs(node.right, level: level + 1, ans: &ans)\n    }\n\n}\n```\n\nLet me know in comments if you have any doubts. I will be happy to answer.\n\nPlease upvote if you found the solution useful.
['Swift']