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 class Solution {\n    public List<List<Integer>> levelOrder(TreeNode root) {\n        Queue<TreeNode> queue = new LinkedList<TreeNode>();\n        List<List<Integer>> wrapList = new LinkedList<List<Integer>>();\n        \n        if(root == null) return wrapList;\n        \n        queue.offer(root);\n        while(!queue.isEmpty()){\n            int levelNum = queue.size();\n            List<Integer> subList = new LinkedList<Integer>();\n            for(int i=0; i<levelNum; i++) {\n                if(queue.peek().left != null) queue.offer(queue.peek().left);\n                if(queue.peek().right != null) queue.offer(queue.peek().right);\n                subList.add(queue.poll().val);\n            }\n            wrapList.add(subList);\n        }\n        return wrapList;\n    }\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 {\n     public:\n        vector<vector<int>> levelOrder(TreeNode* root) {\n            if(root==NULL)\n               return {};\n           vector<vector<int>> ans; \n            queue<TreeNode*> q;\n            q.push(root);\n            TreeNode *temp;\n            int len;\n            while(!q.empty()){\n               len=q.size();\n\t\t       vector<int> v;\n               for(int i=0;i<len;i++){\n                    temp=q.front();\n                    q.pop();\n                    v.push_back(temp->val);\n\n                   if(temp->left) q.push(temp->left);\n                   if(temp->right) q.push(temp->right);\n                }\n\t\t        ans.push_back(v);\n            }\n          return ans;\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```\nclass Solution:\n    def levelOrder(self, root: TreeNode) -> List[List[int]]:\n        queue, ans = deque([root] if root else []), []\n        while len(queue):\n            qlen, row = len(queue), []\n            for _ in range(qlen):\n                curr = queue.popleft()\n                row.append(curr.val)\n                if curr.left: queue.append(curr.left)\n                if curr.right: queue.append(curr.right)\n            ans.append(row)\n        return ans\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```\nvar levelOrder = function(root) {\n    let q = [root], ans = []\n    while (q[0]) {\n        let qlen = q.length, row = []\n        for (let i = 0; i < qlen; i++) {\n            let curr = q.shift()\n            row.push(curr.val)\n            if (curr.left) q.push(curr.left)\n            if (curr.right) q.push(curr.right)\n        }\n        ans.push(row)            \n    }\n    return ans\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```\nclass BinaryTreeLevelOrderTraversal {\n    fun levelOrder(root: TreeNode?): List<List<Int>> {\n        if (root == null) return emptyList()\n        val result = mutableListOf<List<Int>>()\n        val queue = LinkedList<TreeNode>()\n        queue.add(root)\n        while (queue.isNotEmpty()) {\n            result.add(queue.toList().map { it.`val` })\n            for (i in queue.indices) {\n                val cur = queue.poll()\n                cur.left?.let { queue.add(it) }\n                cur.right?.let { queue.add(it) }\n            }\n        }\n        return result\n    }\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    func levelOrder(_ root: TreeNode?) -> [[Int]] {\n        var res: [[Int]] = []\n        var queue: [TreeNode?] = root != nil ? [root] : []\n        var parent\u0421ount = root != nil ? 1 : 0\n        var child\u0421ount = 0\n        var array: [Int] = []\n        var index = 0\n        \n        while parent\u0421ount > 0 {\n            if let node = queue[index] {\n                if node.left != nil {\n                    queue.append(node.left)\n                    child\u0421ount += 1\n                }\n                \n                if node.right != nil {\n                    queue.append(node.right)\n                    child\u0421ount += 1\n                }\n                \n                array.append(node.val)\n                if array.count == parent\u0421ount {\n                    res.append(array)\n                    array = []\n                    parent\u0421ount = child\u0421ount\n                    child\u0421ount = 0\n                }\n                \n                index += 1\n            }\n        }\n        \n        return res\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', 'C++', 'Java', 'Python3', 'Kotlin', 'JavaScript']