题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
vector<int> ans;
if(!root)
return ans;
queue<TreeNode*> temp;
temp.push(root);
while(!temp.empty())
{
TreeNode* cur = temp.front();
ans.push_back(cur->val);
temp.pop();
if(cur->left)
temp.push(cur->left);
if(cur->right)
temp.push(cur->right);
}
return ans;
}
};