标签:return treenode nod tor back cto auto eve 二叉树
从上到下打印二叉树的每一行,最后返回一个层序遍历的序列
vector
里面返回即可class Solution {
public:
vector<int> levelOrder(TreeNode* root) {
if(!root) {
return {};
}
queue<TreeNode*> q;
vector<int> ans;
q.push(root);
while(!q.empty()) {
auto cur = q.front();
q.pop();
ans.emplace_back(cur->val);
if(cur->left) {
q.push(cur->left);
}
if(cur->right) {
q.push(cur->right);
}
}
return ans;
}
};
标签:return treenode nod tor back cto auto eve 二叉树
原文地址:https://www.cnblogs.com/MartinLwx/p/14347162.html