题目:
代码(首刷自解 2024年1月24日):
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res ={};
if(root == nullptr) return res;
queue<TreeNode*> que;
TreeNode* cur = root;
int size = 0;
que.push(cur);
while (!que.empty()) {
int size = que.size();
vector<int> vec = {};
while (size--) {
TreeNode* node = que.front();
que.pop();
vec.emplace_back(node->val);
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
res.emplace_back(vec);
}
reverse(res.begin(),res.end());
return res;
}
};