相似题目:
1 /** 2 * Definition for a binary tree node. 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 if(root==NULL) return {}; 14 queue<TreeNode*> q; 15 TreeNode* front; 16 q.push(root); 17 vector<vector<int>> res; 18 while(!q.empty()){ 19 vector<int> onelevel; 20 for(int i=q.size();i>0;i--){ 21 front=q.front(); 22 q.pop(); 23 if(front->left) 24 q.push(front->left); 25 if(front->right) 26 q.push(front->right); 27 onelevel.push_back(front->val); 28 } 29 res.push_back(onelevel); 30 } 31 reverse(res.begin(),res.end()); 32 return res; 33 } 34 };
其实这个解答只是reverse 了一下,算是投机取巧吧,之后写个从叶节点遍历的。