class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> ans;
if(!root)
return ans;
queue<TreeNode*> aux;
aux.push(root);
while(!aux.empty()){
int len = aux.size();
vector<int> tem;
for(int i = 0; i<len;i++){
TreeNode* cur = aux.front();
tem.push_back(cur->val);
aux.pop();
if(cur->left)
aux.push(cur->left);
if(cur->right)
aux.push(cur->right);
}
ans.push_back(tem);
}
reverse(ans.begin(),ans.end());
return ans;
}
};
广度优先搜索,确定每层的节点个数。