#include <algorithm>
#include <vector>
#include <deque>
//思路,这不就一个层序遍历的事。两个队列来回倒,以便确认这一层什么时候结束。没了。
class Solution {
public:
deque<TreeNode*>q;
deque<TreeNode*>temp;
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> >result;
vector<int>OneLayer;
if (pRoot == nullptr)
return result;
q.push_back(pRoot);
while (!q.empty()) {
TreeNode* current = q.front();
q.pop_front();
OneLayer.push_back(current->val);
if (current->left)temp.push_back(current->left);
if (current->right)temp.push_back(current->right);
if (q.empty()) {//这层结束的标志,队列为空
if (!temp.empty()) {//把下一层的子节点倒过来,当然,也可能pop_front出来的这个节点它没有子节点,那就没的copy了。
copy(temp.begin(), temp.end(), std::back_inserter(q));
}
temp.clear();
result.push_back(OneLayer);
OneLayer.clear();
}
}
return result;
}
};