q.push(root); vector<int> inner; while (!q.empty()) { int len = q.size(); /* 关键代码 */ for (int i = 0; i < len; i++) { TreeNode* tmp = q.front(); q.pop();
if (tmp != NULL) { inner.push_back(tmp->val); q.push(tmp->left); q.push(tmp->right); } } if (!inner.empty()) { res.push_back(inner); inner.clear(); } } return res; }