题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印
思路:广度优先遍历
1 class Solution { 2 public: 3 vector<int> PrintFromTopToBottom(TreeNode* root) { 4 vector<int> res; 5 if(root==NULL)return res; 6 std::queue<TreeNode*> que; 7 que.push(root); 8 while(que.size()) 9 { 10 TreeNode *tmp=que.front(); 11 que.pop(); 12 res.push_back(tmp->val); 13 if(tmp->left)que.push(tmp->left); 14 if(tmp->right)que.push(tmp->right); 15 } 16 return res; 17 } 18 };