/*
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>ret;
if (!root) return ret;
queue<TreeNode*> q;
q.push(root);
while(!q.empty())
{
int s = q.size();
while(s--)
{
TreeNode *node = q.front();
q.pop();
ret.push_back(node->val);
if(node->left) q.push(node->left);
if(node->right) q.push(node->right);
}
}
return ret;
}
};
剑指数据结构-从上到下打印二叉树
最新推荐文章于 2024-07-14 14:13:59 发布