利用递归的特性来实现:
class Solution { public: /** * * @param root TreeNode类 * @return int整型vector<vector<>> */ vector<vector<int> > levelOrderBottom(TreeNode *root) { // write code here vector<vector<int> > res; if (!root) return res; queue<TreeNode *> qu; qu.push(root); bottomToTop(qu, res); return res; } void bottomToTop(queue<TreeNode *> qu, vector<vector<int> > &res) { if (qu.empty()) return; vector<int> vec; int size = qu.size(); for (int i = 0; i < size; ++i) { TreeNode *node = qu.front(); qu.pop(); vec.push_back(node->val); if (node->left) qu.push(node->left); if (node->right) qu.push(node->right); } bottomToTop(qu, res); res.push_back(vec); } };