和104题目相似,区别在于本题树的层数由低到高存储。
代码:
class Solution {
public:
vector<vector<int>> result;
void trval(TreeNode* t,int level)
{
int maxlevel=result.size();
if(maxlevel<level)
{
vector<int>temp;
temp.push_back(t->val);
result.insert(result.begin(),temp);
}
else
{
result[maxlevel-level].push_back(t->val);
}
if(t->left!=NULL)
trval(t->left, level+1);
if(t->right!=NULL)
trval(t->right, level+1);
}
vector<vector<int>> levelOrderBottom(TreeNode* root){
if(root!=NULL)
trval(root, 1);
return result;
}
};