广度优先搜索
# include <iostream>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
using namespace std;
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
queue<TreeNode*>Q;
vector<int> PrintFromTopToBottom(TreeNode* root) {
vector<int>res;
if(root == nullptr)//!!!!判断参数合法性,不然提交一直错误
return res;
res.push_back (root->val);
Q.push(root);
while(!Q.empty())
{
TreeNode * temp = Q.front();
Q.pop();
if(temp->left)
{
res.push_back (temp->left->val);
Q.push(temp->left);
}
if(temp->right)
{
res.push_back (temp->right->val);
Q.push(temp->right);
}
}
return res;
}
};