# LeetCode-Binary Tree Level Order Traversal-解题报告

Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

For example:
Given binary tree {3,9,20,#,#,15,7},

    3
/ \
9  20
/  \
15   7


return its level order traversal as:

[
[3],
[9,20],
[15,7]
]


/**
* Definition for a binary tree node.
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > levelOrder(TreeNode* root) {
vector<vector<int> >ans;
queue<TreeNode*>Q;
vector<int>tmp;
if (root == NULL)return ans;
Q.push(root);
while (!Q.empty())
{
tmp.clear();
int size = Q.size();
for (int i = 0; i < size; ++i)
{
TreeNode* t = Q.front();
Q.pop();
tmp.push_back(t->val);
if (t->left != NULL)Q.push(t->left);
if (t->right != NULL)Q.push(t->right);
}
ans.push_back(tmp);
}
return ans;
}
};

• 本文已收录于以下专栏：

举报原因： 您举报文章：LeetCode-Binary Tree Level Order Traversal-解题报告 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)