32-1从上到下打印二叉树

class Solution {
public:
vector PrintFromTopToBottom(TreeNode* root) {
queue>q;
vectorans;
if(!root)return ans;//可能为空树
q.push(root);
while(!q.empty())
{
TreeNode top=q.front();
q.pop();
ans.push_back(top->val);
if(top->left)q.push(top->left);
if(top->right)q.push(top->right);
}
return ans;
}
};

你可能感兴趣的:(32-1从上到下打印二叉树)