二叉树层次遍历(queue)
从上往下打印二叉树
http://www.nowcoder.com/questionTerminal/7fe2212963db4790b57431d9ed259701
/* 层次遍历 */ class Solution { public: vector<int> PrintFromTopToBottom(TreeNode* root) { vector<int> vt; if(!root)return vt;//push()进去的不能为空 queue<TreeNode*> q; q.push(root); while(!q.empty()){ TreeNode* tmp=q.front();q.pop(); vt.push_back(tmp->val); if(tmp->left)q.push(tmp->left); if(tmp->right)q.push(tmp->right); } return vt; } };