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