题目:
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree {3,9,20,#,#,15,7}
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7] [9,20], [3], ]
代码如下:
void getleverorder(TreeNode *root,vector<vector<int> > &getnode,int i)
{
if(root!=NULL)
{
if(i>getnode.size())
{
vector<int> tmp;
tmp.push_back(root->val);
getnode.push_back(tmp);
}
else
{
getnode[i-1].push_back(root->val);
}
getleverorder(root->left,getnode,i+1);
getleverorder(root->right,getnode,i+1);
}
return;
}
vector<vector<int> > levelOrderBottom(TreeNode *root) {
vector<vector<int> > getnode;
vector<vector<int> > result;
int i=1;
getleverorder(root,getnode,i);
for(i=getnode.size()-1;i>=0;i--)
{
result.push_back(getnode[i]);
}
return result;
}