class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> res = {};
TreeNode* curNode = root;
queue<pair<TreeNode*, int>> helper;
if (!root) return res;
helper.push(make_pair(curNode, 0));
while(!helper.empty()){
curNode = helper.front().first;
int i = helper.front().second;
helper.pop();
if (i >= res.size()) res.push_back( vector<int>() );
res[i].push_back(curNode->val);
if (curNode->left) helper.push(make_pair(curNode->left, i+1));
if (curNode->right) helper.push(make_pair(curNode->right, i+1));
}
return res;
}
};