class Solution {
/**
* @param root : The root of binary tree.
* @return : buttom-up level order a list of lists of integer
*/
public:
stack<vector<int>> a;
vector<vector<int>> b;
queue<TreeNode*> q;
vector<int> a1;
void solution(TreeNode *root)
{
q.push(root);
while(!q.empty())
{
TreeNode *x;
x=new TreeNode;
a1.clear();
int length1=q.size();
for(int i=1;i<=length1;i++)
{
x=q.front();
a1.push_back(x->val);
if(x->left!=NULL)
q.push(x->left);
if(x->right!=NULL)
q.push(x->right);
q.pop();
}
a.push(a1);
}
}
void solution2()
{
while(!a.empty())
{
b.push_back(a.top());
a.pop();
}
}
vector<vector<int>> levelOrderBottom(TreeNode *root) {
if(root==NULL)
return b;
solution(root);
solution2();
return b;
// write your code here
}
};
二叉树的层次遍历 II - C++
最新推荐文章于 2024-05-15 00:15:33 发布