/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> v; queue<TreeNode*> q; q.push(root); if(!root) return v; while(!q.empty()) { vector<int> v2; int p1=q.size(); while(p1--) { TreeNode* temp=q.front(); q.pop(); v2.push_back(temp->val); if(temp->left) q.push(temp->left); if(temp->right) q.push(temp->right); } v.push_back(v2); } reverse(v.begin(), v.end()); return v; } };