AcWing 43. 不分行从上往下打印二叉树
原题链接
简单
作者:
daniellee
,
2019-04-10 20:38:51
,
所有人可见
,
阅读 868
C++ 代码
/**
* 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> printFromTopToBottom(TreeNode* root) {
vector<int> v;
if (root==NULL) return v;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
TreeNode* c = q.front();
q.pop();
v.push_back(c->val);
if (c->left!=NULL){
q.push(c->left);
}
if (c->right!=NULL){
q.push(c->right);
}
}
return v;
}
};