class Solution {
public:
vector<vector<int> > levelOrder(TreeNode *root) {
queue<TreeNode*>q;
vector<vector<int>>ans;
if(!root)return ans;
q.push(root);
while(!q.empty())
{
int size=q.size();
vector<int>temp;
while(size--)
{
TreeNode*top=q.front();
q.pop();
temp.push_back(top->val);
if(top->left)q.push(top->left);
if(top->right)q.push(top->right);
}
ans.push_back(temp);
}
return ans;
}
};
网友评论