/**
* Definition for a binary tree node.
* 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>> v;
if (!root) {
return v;
}
queue<TreeNode*> q;
vector<int> vTemp;
vTemp.push_back(root->val);
v.insert(v.begin(), vTemp);
q.push(root);
while (!q.empty()){
vector<int> vTemp;
int size = q.size();
for(int i = 0 ; i < size; i++){
TreeNode* node = q.front();
q.pop();
if (node->left){
vTemp.push_back(node->left->val);
q.push(node->left);
}
if (node->right){
vTemp.push_back(node->right->val);
q.push(node->right);
}
}
if (!vTemp.empty()){
v.insert(v.begin(), vTemp);
}
}
return v;
}
};