从上往下打印出二叉树的每个节点,同层节点从左至右打印。
/*
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>vt;
if(root==NULL)
return vt;
queue<TreeNode*>qt;
qt.push(root);
while(!qt.empty())
{
root=qt.front();
qt.pop();
vt.push_back(root->val);
if(root->left) qt.push(root->left);
if(root->right) qt.push(root->right);
}
return vt;
}
};