层次遍历
/*
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) {
std::vector<int> res;
if (root == nullptr) {
return res;
}
std::queue<TreeNode *> tree;
tree.push(root);
while (!tree.empty()) {
int size = tree.size();
for (int i = 0; i < size; ++i) {
TreeNode *tmp = tree.front();
tree.pop();
res.push_back(tmp->val);
if (tmp->left) {
tree.push(tmp->left);
}
if (tmp->right) {
tree.push(tmp->right);
}
}
}
return res;
}
};