Easy C++ solution || Recursive and iterative approach || Beats 100% !!
Code\n\n// Iterative approach\nclass Solution {\npublic:\n    vector<vector<int>> levelOrder(TreeNode* root) {\n        vector<vector<int>> ans; \n        if(ro
# Code\n```\n// Iterative approach\nclass Solution {\npublic:\n    vector<vector<int>> levelOrder(TreeNode* root) {\n        vector<vector<int>> ans; \n        if(root == NULL)    return ans;\n\n        queue<TreeNode*> q;\n        q.push(root);\n\n        while(!q.empty()){\n            int size = q.size();\n            vector<int> res;\n            for(int i=0; i<size; i++){\n                TreeNode *temp = q.front();\n                q.pop();\n                res.push_back(temp->val);\n\n                if(temp->left != NULL)\n                    q.push(temp->left);\n                if(temp->right != NULL)\n                    q.push(temp->right);\n            }\n            ans.push_back(res);\n        }\n        return ans;\n    }\n};\n\n// Recursive approach\nclass Solution {\npublic:\n    vector<vector<int>> ans;\n\n    void solve(TreeNode *root, int depth){\n        // base case\n        if(root == NULL) \n            return;\n        \n        if(ans.size() == depth)\n            ans.push_back(vector<int>());\n    \n        ans[depth].push_back(root->val);\n        solve(root->left, depth + 1);\n        solve(root->right, depth + 1);\n    }\n\n    vector<vector<int> > levelOrder(TreeNode *root) {\n        solve(root, 0);\n        return ans;\n    }\n};\n```
['Tree', 'Breadth-First Search', 'Queue', 'Binary Tree', 'C++']