/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>> res;
queue<TreeNode *> Q;
if(root) Q.push(root);
while( !Q.empty())
{
int count=0;
int levCount=Q.size();
vector<int> levNode;
while(count<levCount)
{
TreeNode *curNode=Q.front();
Q.pop();
levNode.push_back(curNode->val);
if(curNode->left)
Q.push(curNode->left);
if(curNode->right)
Q.push(curNode->right);
count++;
}
res.push_back(levNode);
}
reverse(res.begin(),res.end()); //在上一题的基础上增加一行
return res;
}
};
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution
{
public:
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>> res;
queue<TreeNode *> Q;
if(root) Q.push(root);
stack<vector<int>> stk;
while( !Q.empty())
{
int count=0;
int levCount=Q.size();
vector<int> levNode;
while(count<levCount)
{
TreeNode *curNode=Q.front();
Q.pop();
levNode.push_back(curNode->val);
if(curNode->left)
Q.push(curNode->left);
if(curNode->right)
Q.push(curNode->right);
count++;
}
stk.push(levNode); //压入栈
}
while(!stk.empty()) //出栈
{
res.push_back(stk.top());
stk.pop();
}
return res;
}
};