leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0107.cpp (656B)
0 class Solution { 1 public: 2 vector<vector<int>> levelOrderBottom(TreeNode *root) { 3 if (!root) return {}; 4 5 vector<vector<int>> res; 6 queue<TreeNode *> q; 7 8 q.push(root); 9 for (int lvl = 0; !q.empty(); lvl++) { 10 res.push_back(vector<int>()); 11 for (int t = q.size(); t > 0; t--) { 12 TreeNode *root = q.front(); 13 q.pop(); 14 res[lvl].push_back(root->val); 15 if (root->left) q.push(root->left); 16 if (root->right) q.push(root->right); 17 } 18 } 19 reverse(res.begin(), res.end()); 20 return res; 21 } 22 };