https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/
思路:即可,每次處理一整個層次。
/**
* 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<int> levelOrder(TreeNode* root) {
vector<int> ans;
if(root==NULL)
return ans;
queue<TreeNode*> q;
q.push(root);
TreeNode *fon;
int siz;
while(!q.empty())
{
siz=q.size();
while(siz--)
{
fon=q.front();
q.pop();
ans.push_back(fon->val);
if(fon->left!=NULL)
q.push(fon->left);
if(fon->right!=NULL)
q.push(fon->right);
}
}
return ans;
}
};