Simple Using C++ and BFS


#1

vector<vector > Solution::levelOrder(TreeNode* root) {
vector<vector > v;
if(root==NULL)return v;
queue<TreeNode*> q;
q.push(root);
vector temp;
while(!q.empty())
{
int n=q.size();
temp.clear();
while(n–)
{
TreeNode *t=q.front();q.pop();
temp.push_back(t->val);
if(t->left)
q.push(t->left);
if(t->right)
q.push(t->right);
}
v.push_back(temp);

}
return v;

}