#include <iostream> #include <vector> #include <stack> #include <queue> using namespace std; 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) { result.clear(); if (!root) return result; queue<TreeNode *> q; q.push(root); queue<TreeNode *> tmpQ; while(!q.empty()){ while(!q.empty()){ tmpQ.push(q.front()); q.pop(); } vector<int> oneLevel; while(!tmpQ.empty()){ oneLevel.push_back(tmpQ.front()->val); if (tmpQ.front()->left) q.push(tmpQ.front()->left); if (tmpQ.front()->right) q.push(tmpQ.front()->right); tmpQ.pop(); } result.insert(result.begin(), oneLevel);//对于每一层,既然想从右到左遍历,那么就改变一下顺序就好了 //result.push_back(oneLevel); } return result; } private: vector<vector<int> > result; }; int main() { return 0; }
EOF