Beats100%(Recursive approach)✅ | O( n)✅ | (Step by step explanation)✅
Intuition\nThe problem is to perform a level-order traversal of a binary tree and return the values of nodes\' values in the order from left to right, level by 
# Intuition\nThe problem is to perform a level-order traversal of a binary tree and return the values of nodes\' values in the order from left to right, level by level.\n\n# Approach\nThe approach is to perform a modified depth-first traversal of the tree. We define a helper function `level` that takes the current node, the current level, and a vector of vectors to store the result. During the traversal, if the result vector is not long enough to accommodate the current level, we add a new subarray to store the values at that level.\n\n1. **Initialize**: Start with an empty vector `result` to store the level order traversal.\n2. **Recursive Helper Function**: Define a recursive helper function `level` that takes the current node, the current level, and a vector of vectors to store the result.\n3. **Base Case**: In the `level` function, if the current node is `nullptr`, return, as there is nothing to process.\n4. **Ensure Enough Subarrays**: Check if the `result` vector is long enough to accommodate the current level. If not, add a new subarray to store the values at that level.\n5. **Add Node Value**: Add the value of the current node to the subarray corresponding to the current level.\n6. **Recursive Calls**: Recursively call the `level` function for the left and right children of the current node, incrementing the level by 1 for each call.\n7. **Return**: After the traversal is complete, the `result` vector contains subarrays representing the nodes at each level of the tree.\n\n# Complexity\n- Time complexity: O(n)\n  - We visit each node exactly once during the traversal.\n- Space complexity: O(h)\n  - The space complexity is determined by the recursion stack.\n  - In the worst case, when the tree is skewed, the space complexity is O(n).\n\n# Code\n\n\n<details open>\n  <summary>Python Solution</summary>\n\n```python\nclass Solution:\n    def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n        result = []\n        count = 0\n\n        def level(count , root):\n            if root == None :\n                return \n\n            if len(result) <= count:\n                result.append([])    \n            \n            result[count].append(root.val)\n            count += 1\n            level(count , root.left)\n            level(count , root.right) \n\n        level(count , root)\n        return result\n```\n</details>\n\n<details open>\n  <summary>C++ Solution</summary>\n\n```cpp\nclass Solution {\npublic:\n    vector<vector<int>> levelOrder(TreeNode* root) {\n        vector<vector<int>> result;\n        int count = 0;\n        level(root , count , result);\n        return result;\n\n    }\nprivate:\n    void level(TreeNode* root , int count , vector<vector<int>> &result){\n        if(root == NULL){\n            return ;\n        }\n\n        if(result.size() <= count){\n            result.push_back(vector<int>());\n        }\n        result[count].push_back(root->val);\n        count++;\n        level(root->left , count , result);\n        level(root->right , count , result);\n  \n    }    \n};\n```\n</details>\n\n# Please upvote the solution if you understood it.\n![1_3vhNKl1AW3wdbkTshO9ryQ.jpg](https://assets.leetcode.com/users/images/0161dd3b-ad9f-4cf6-8c43-49c2e670cc21_1699210823.334661.jpeg)\n\n\n\n# Please upvote the solution if you understood it.\n![1_3vhNKl1AW3wdbkTshO9ryQ.jpg](https://assets.leetcode.com/users/images/0161dd3b-ad9f-4cf6-8c43-49c2e670cc21_1699210823.334661.jpeg)\n\n
['Tree', 'Depth-First Search', 'Recursion', 'C++', 'Python3']