102. Binary Tree Level Order Traversal
- if
pre/in/post
wereDFS
type then this is kind ofBFS
type. - there we used stack, here it will be queue.
code
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> ans;
if (root == nullptr) return {};
queue<TreeNode*> qu;
qu.push(root);
while (!qu.empty()) {
vector<int> temp;
int Size = qu.size();
for (int i = 0; i < Size; i++) {
auto root = qu.front(); qu.pop();
temp.push_back(root -> val);
if (root -> left) qu.push(root -> left);
if (root -> right) qu.push(root -> right);
}
ans.push_back(temp);
}
return ans;
}
};