给定一个二叉树,返回该二叉树由底层到顶层的层序遍历,(从左向右,从叶子节点到根节点,一层一层的遍历)
例如:
给定的二叉树是{3,9,20,#,#,15,7},
!
牛客原题,直接上代码 C++
```cpp
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
#include <queue>
class Solution {
public:
/**
* @param root TreeNode类
* @return int整型vector<vector<>>
*/
vector<vector<int> > levelOrderBottom(TreeNode* root) {
// write code here
vector<vector<int>>sta;
if(root == NULL)
return sta;
vector<int> vec;
queue<TreeNode*>q;
q.push(root);
while(!q.empty())
{
int size =q.size();
vector<int> vec1;
for(int i=0;i<size;i++)
{
TreeNode* tem = q.front();
vec1.push_back(tem->val);
if(tem->left!=NULL)
q.push(tem->left);
if(tem->right!=NULL)
q.push(tem->right);
q.pop();
}
sta.push_back(vec1);
}
int m = sta.size();
for(int i=0;i<m/2;i++)
{
vector<int> tem = sta[i];
sta[i]=sta[m-1-i];
sta[m-1-i]=tem;
}
return sta;
}
};