O(n)time/BEATS 99.97% MEMORY/SPEED 0ms MAY 2022
\n\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, please upvote this post.)\nTake care broth
```\n```\n\n(Note: This is part of a series of Leetcode solution explanations. If you like this solution or find it useful, ***please upvote*** this post.)\n***Take care brother, peace, love!***\n\n```\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 38.2MB*** (beats 92.04% / 24.00%).\n* ***Java***\n```\npublic List<List<Integer>> zigzagLevelOrder(TreeNode root) {\n   TreeNode c=root;\n   List<List<Integer>> ans =new ArrayList<List<Integer>>();\n   if(c==null) return ans;\n   Stack<TreeNode> s1=new Stack<TreeNode>();\n   Stack<TreeNode> s2=new Stack<TreeNode>();\n   s1.push(root);\n   while(!s1.isEmpty()||!s2.isEmpty())\n   {\n       List<Integer> tmp=new ArrayList<Integer>();\n        while(!s1.isEmpty())\n        {\n            c=s1.pop();\n            tmp.add(c.val);\n            if(c.left!=null) s2.push(c.left);\n            if(c.right!=null) s2.push(c.right);\n        }\n        ans.add(tmp);\n        tmp=new ArrayList<Integer>();\n        while(!s2.isEmpty())\n        {\n            c=s2.pop();\n            tmp.add(c.val);\n            if(c.right!=null)s1.push(c.right);\n            if(c.left!=null)s1.push(c.left);\n        }\n        if(!tmp.isEmpty()) ans.add(tmp);\n   }\n   return ans;\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***0ms / 7.0MB*** (beats 100.00% / 100.00%).\n* ***C++***\n```\nclass Solution {\npublic:\n    void helper(TreeNode*root,vector<vector<int>>&res,int level)\n    {\n        if(!root) return;   \n        if(level>=res.size())\n            res.push_back({});\n        res[level].push_back(root->val);\n        helper(root->left,res,level+1);\n        helper(root->right,res,level+1);\n    }\n    vector<vector<int>> zigzagLevelOrder(TreeNode* root) {\n        vector<vector<int>>res;\n        if(!root)   return res;\n        helper(root,res,0); \n        for(int i=1;i<res.size();i=i+2)\n        {\n            reverse(res[i].begin(),res[i].end());\n        }\n        return res;\n    }\n};\n```\n\n```\n```\n\n```\n```\n\n\nThe best result for the code below is ***26ms / 12.2MB*** (beats 95.42% / 82.32%).\n* ***Python***\n```\ndef zigzagLevelOrder(self, root: TreeNode) -> List[List[int]]:\n\tif not root: return []\n\tqueue = collections.deque([root])\n\tres = []\n\teven_level = False\n\twhile queue:\n\t\tn = len(queue)\n\t\tlevel = []\n\t\tfor _ in range(n):\n\t\t\tnode = queue.popleft()\n\t\t\tlevel.append(node.val)\n\t\t\tif node.left:\n\t\t\t\tqueue.append(node.left)\n\t\t\tif node.right:\n\t\t\t\tqueue.append(node.right)\n\t\tif even_level:\n\t\t\tres.append(level[::-1])\n\t\telse:\n\t\t\tres.append(level)\n\t\teven_level = not even_level\n\treturn res\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***51ms / 34.2MB*** (beats 100.00% / 84.12%).\n* ***JavaScript***\n```\nfunction zigzagLevelOrder(root) {\n  let res = [];\n  go(root, 0, res);\n  return res;\n}\n\nfunction go(node, l, res) {  // l means level\n  if (!node) return;\n\n  if (res[l] == null) {\n    res.push([]);\n  }\n\n  if (l % 2 === 0) {\n    res[l].push(node.val);\n  } else {\n    res[l].unshift(node.val);\n  }\n\n  go(node.left, l + 1, res);\n  go(node.right, l + 1, res);\n}\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***68ms / 44.2MB*** (beats 100.00% / 45.25%).\n* ***Kotlin***\n```\n fun zigzagLevelOrder(root: TreeNode?): List<List<Int>> {\n        val queue : Queue<TreeNode> = LinkedList()\n        val result = mutableListOf<LinkedList<Int>>()\n        if (root == null) return result\n        var leftToRight = true // if true, add elements in normally else add in reverse\n        queue.add(root)\n        while (queue.isNotEmpty()) {\n            val thisLevel = LinkedList<Int>()\n            var size = queue.size\n            while (size > 0) {\n                var current = queue.poll()\n                if (leftToRight.not()) thisLevel.addFirst(current.`val`) // reverse\n                else thisLevel.add(current.`val`) // normal\n                current.left?.let { queue.offer(it) }\n                current.right?.let { queue.offer(it) }\n                --size\n            }\n            result.add(thisLevel)\n            leftToRight = leftToRight.not()\n        }\n        return result\n    }\n```\n\n```\n```\n\n```\n```\n\nThe best result for the code below is ***12ms / 32.2MB*** (beats 95% / 84%).\n* ***Swift***\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\n```\n```\n\n```\n```\n\n***"Open your eyes. Expect us." - \uD835\uDCD0\uD835\uDCF7\uD835\uDCF8\uD835\uDCF7\uD835\uDD02\uD835\uDCF6\uD835\uDCF8\uD835\uDCFE\uD835\uDCFC***
['Breadth-First Search', 'Queue', 'Swift', 'C', 'Python', 'Java', 'Python3', 'Kotlin', 'JavaScript']