Best Approach is Here!!! beats 100%
Intuition\nThe level order traversal of a binary tree requires visiting nodes level by level from left to right. This can be achieved using a queue data structu
## Intuition\nThe level order traversal of a binary tree requires visiting nodes level by level from left to right. This can be achieved using a queue data structure which supports FIFO (First In First Out) operations. By processing nodes level by level and maintaining a queue to track nodes at the current level, we can ensure all nodes are visited in the correct order.\n\n## Approach\n1. **Initialization**: \n   - Check if the root is `None`. If it is, return an empty list as the tree is empty.\n   - Initialize an empty list `ans` to store the final result.\n   - Initialize a queue with the root node.\n\n2. **Processing Levels**:\n   - Use a `while` loop to continue processing as long as there are nodes in the queue.\n   - For each level, determine the number of nodes at that level (`level_size`) by the current size of the queue.\n   - Initialize an empty list `level` to store the node values for the current level.\n   - Use a `for` loop to iterate over the nodes at the current level:\n     - Dequeue the node from the front of the queue.\n     - Append the node\'s value to the `level` list.\n     - Enqueue the node\'s left child if it exists.\n     - Enqueue the node\'s right child if it exists.\n   - After processing all nodes at the current level, append the `level` list to `ans`.\n\n3. **Return Result**:\n   - Return the list `ans` which contains lists of node values for each level.\n\nHere is the code that implements this approach:\n\n```python []\n\nclass Solution:\n    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n        if not root:\n            return []\n        \n        ans = []\n        queue = [root]\n        \n        while queue:\n            level_size = len(queue)\n            level = []\n            \n            for i in range(level_size):\n                node = queue.pop(0)\n                level.append(node.val)\n                \n                if node.left:\n                    queue.append(node.left)\n                if node.right:\n                    queue.append(node.right)\n            \n            ans.append(level)\n        \n        return ans\n\n```\n```java []\nclass Solution {\n    public List<List<Integer>> levelOrder(TreeNode root) {\n        List<List<Integer>> ans = new ArrayList<>();\n        if (root == null) return ans;\n\n        Queue<TreeNode> queue = new LinkedList<>();\n        queue.add(root);\n\n        while (!queue.isEmpty()) {\n            int levelSize = queue.size();\n            List<Integer> level = new ArrayList<>();\n\n            for (int i = 0; i < levelSize; ++i) {\n                TreeNode node = queue.poll();\n                level.add(node.val);\n\n                if (node.left != null) queue.add(node.left);\n                if (node.right != null) queue.add(node.right);\n            }\n\n            ans.add(level);\n        }\n\n        return ans;\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    vector<vector<int>> levelOrder(TreeNode* root) {\n        vector<vector<int>> ans;\n        if (!root) return ans;\n\n        queue<TreeNode*> q;\n        q.push(root);\n\n        while (!q.empty()) {\n            int level_size = q.size();\n            vector<int> level;\n\n            for (int i = 0; i < level_size; ++i) {\n                TreeNode* node = q.front();\n                q.pop();\n                level.push_back(node->val);\n\n                if (node->left) q.push(node->left);\n                if (node->right) q.push(node->right);\n            }\n\n            ans.push_back(level);\n        }\n\n        return ans;\n    }\n};\n```\n\n\n## Complexity\n- **Time complexity**: \\(O(n)\\)\n  - Each node is processed exactly once (enqueued and dequeued once), where \\(n\\) is the number of nodes in the tree.\n  \n- **Space complexity**: \\(O(n)\\)\n  - The space required for the result list `ans` is proportional to the number of nodes.\n  - The queue can hold at most the number of nodes at the largest level, which in the worst case can be \\(O(n)\\).\n\n  ![upvote.jpg](https://assets.leetcode.com/users/images/11037f67-72d2-4fd4-8fed-1b957d56e92c_1719809514.1699276.jpeg)\n
['Tree', 'Breadth-First Search', 'C++', 'Java', 'Python3']