给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)
class TreeNode
{
public:
int val;
TreeNode *left;
TreeNode *right;
TreeNode():val(NULL),left(nullptr),right(nullptr){}
TreeNode(int x):val(x),left(nullptr),right(nullptr){}
};
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root)
{
if(root == nullptr) return res;
que.push(root);
while(!que.empty())
{
int size = que.size();
vector<int> vec;
for(int i = 0; i < size ; i++)
{
TreeNode *node = que.front();
vec.push_back(node->val);
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
res.push_back(vec);
}
return res;
}
private:
// stack<TreeNode*> sta;
queue<TreeNode*> que;
vector<vector<int>> res;
};
标签:node,right,TreeNode,层序,nullptr,push,que,二叉树,102
From: https://www.cnblogs.com/lihaoxiang/p/17281072.html