-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP107.cpp
More file actions
25 lines (22 loc) · 768 Bytes
/
P107.cpp
File metadata and controls
25 lines (22 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res={};
if (!root) return res;
vector<TreeNode*> que;
que.push_back(root);
while (!que.empty()) {
vector<int> ans;
vector<TreeNode*> next;
for (size_t i=0;i<que.size();++i) {
ans.push_back(que[i]->val);
if (que[i]->left) next.push_back(que[i]->left);
if (que[i]->right) next.push_back(que[i]->right);
}
res.push_back(ans);
que=next;
}
reverse(res.begin(), res.end());
return res;
}
};