It is my AC solution.
Here i use bfs with queue. The type of queue is pair where the first argument is tree node and second is level number. Can I or is it right to use pair here ?\n
Here i use bfs with queue. The type of queue is pair where the first argument is tree node and second is level number. Can I or is it right to use pair here ?\n\n    class Solution {\n    public:\n        vector<vector<int> > levelOrder(TreeNode *root) {\n            vector< vector<int> > ans;\n            if (!root) return ans;\n            queue<pair<TreeNode *, int> > q;\n            q.push(make_pair(root, 0));\n            while (!q.empty()) {\n                \n                int i = q.front().second;\n                TreeNode * node = q.front().first;\n                if (ans.size() >= i) ans.resize(i + 1);\n                ans[i].push_back(node -> val);\n                if (node -> left) q.push(make_pair(node -> left, i + 1));\n                if (node -> right) q.push(make_pair(node -> right, i + 1));\n                q.pop();\n            }\n            return ans;\n        }\n    \n    };
[]