#include<iostream>
#include<queue>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
//在I的基础上 加上了层次区分 我这里处理是用多加一个尾节点来进行区分
class Solution {
queue<TreeNode*> q;//利用队列进行广度优先搜索形成层序遍历
vector<vector<int>> m;//储存值
vector<int>t;//二维数组vector必须进行整体赋值
public:
vector<vector<int>> levelOrder(TreeNode* root) {
if(root==NULL)//如果根节点为空就不用搜了
return m;
auto ED=new TreeNode(-1000);//每一层尾节点 判断是否到下一层
q.push(root);
q.push(ED);
while(!q.empty())//队列遍历模板
{
auto p=q.front();
q.pop();
if(p!=NULL&&p!=ED)//不为空和尾节点时赋值
{
t.push_back(p->val);
q.push(p->left);
q.push(p->right);
}
if(p==ED)
{
if(t.size()!=0)//不为空时进行赋值
m.push_back(t);
t.clear();//清空
if(q.size()!=0)//不为空时加个尾节点
q.push(ED);
}
}
return m;
}
};
int main()
{
}
05-07
902

05-17
05-17
03-31