1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int> > levelOrderBottom(TreeNode *root) { 13 // Start typing your C/C++ solution below 14 // DO NOT write int main() function 15 vector<vector<int> > r; 16 queue<TreeNode *> q,p,useless; 17 18 19 20 TreeNode * t; 21 if(root==NULL) 22 return r; 23 24 q.push(root); 25 26 while(!q.empty()) 27 { 28 vector<int> v; 29 while(!q.empty()) 30 { 31 32 t=q.front(); 33 v.push_back(t->val); 34 q.pop(); 35 if(t->left) 36 p.push(t->left); 37 if(t->right) 38 p.push(t->right); 39 } 40 q=p; 41 p=useless; 42 r.push_back(v); 43 } 44 45 reverse(r.begin(),r.end()); 46 47 48 49 return r; 50 } 51 };