思路:
和下面这道题一样,最后把res反转就可以了。
LeetCode 102. 二叉树的层序遍历
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
//bfs
queue<TreeNode*> q;
if(root) q.push(root);
vector<vector<int>> res;
while(q.size()){
vector<int> level;
int len=q.size();
while(len--){
auto t=q.front();
q.pop();
level.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
res.push_back(level);
}
reverse(res.begin(),res.end());
return res;
}
};
评论区