题目链接:https://leetcode-cn.com/problems/binary-tree-level-order-traversal-ii/ 题目如下:
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> result;
if(root==NULL) return result;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
int size=que.size();
vector<int> temp;
while(size--){
TreeNode* node=que.front();
que.pop();
temp.push_back(node->val);
if(node->left!=NULL) que.push(node->left);
if(node->right!=NULL) que.push(node->right);
}
result.push_back(temp);
}
reverse(result.begin(),result.end());
return result;
}
};
|