跟之前的题一样,直接转vector
/** * 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) { queue<TreeNode*>qu; vector<vector<int>>ve; if(root == NULL) return ve; vector<int>vec; int num = 1; qu.push(root); while(!qu.empty()){ TreeNode* t; vec.clear(); int nn = num; num = 0; for(int i = 1; i <= nn; ++ i){ t = qu.front(); qu.pop(); vec.push_back(t -> val); if(t -> left != NULL){ num++; qu.push(t -> left); } if(t -> right != NULL){ num++; qu.push(t -> right); } } ve.push_back(vec); } int n = ve.size(); for(int i = 0; i < ve.size() / 2; ++ i){ vec = ve[i]; ve[i] = ve[n - i - 1]; ve[n - i - 1] = vec; } return ve; }};新闻热点
疑难解答