interviewalgo 21-03, BFS, LeetCode, Medium, Tree 2021-05-16 Source Edit History 102. 二叉树的层序遍历 IntroductionQuestion:102. 二叉树的层序遍历 Analysis比较简单,直接用队列做广度优先遍历就好了。 Implement1234567891011121314151617vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; if (root==nullptr) return res; queue<TreeNode *> q; q.push(root); while(!q.empty()) { vector<int> vec; for(int i = 0, size = q.size(); i < size; i++) { root = q.front(); q.pop(); vec.push_back(root->val); if (root->left) q.push(root->left); if (root->right) q.push(root->right); } res.push_back(vec); } return res;}