# Share my iterative solution using C++，easy to understand

• ``````class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ret;
TreeNode *temp = NULL;
int curLevelCount = 0;
if (root == NULL) return ret;

queue<TreeNode *>q;
q.push(root);

while (!q.empty())
{
curLevelCount = q.size();
vector<int> level;
while (curLevelCount--)
{
temp = q.front();
level.push_back(temp->val);
if (temp->left != NULL)
q.push(temp->left);
if (temp->right != NULL)
q.push(temp->right);
q.pop();
}
ret.push_back(level);
}

return ret;
}
};``````

• ``````class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ret;

if (root == NULL)
return ret;

vector<TreeNode *> level;
level.push_back(root);
int cur = 0, last = 1;

while (cur < last)
{
vector<int> tmp;

while (cur < last)
{
tmp.push_back(level[cur]->val);

if (level[cur]->left != NULL)
level.push_back(level[cur]->left);

if (level[cur]->right != NULL)
level.push_back(level[cur]->right);

++cur;
}

ret.push_back(tmp);
last = level.size();
}

return ret;
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.