标签:
题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
/*
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> result;
queue<TreeNode *>s;
s.push(root);
if(root==NULL)
return result;
while(!s.empty())
{
//cout<<p->data<<" ";
result.push_back(root->val);
if(root->left!=NULL)
s.push(root->left);
if(root->right!=NULL)
s.push(root->right);
s.pop();
if(!s.empty())///////////////////判断
root=s.front();
}
//return ;
return result;
}
};
标签:
原文地址:http://blog.csdn.net/a819721810/article/details/45920535