-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path107.cpp
More file actions
27 lines (27 loc) · 732 Bytes
/
Copy path107.cpp
File metadata and controls
27 lines (27 loc) · 732 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
26
27
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode *root) {
vector<vector<int>> ret;
if (!root)
return ret;
vector<int> cur;
int len = 1;
queue<TreeNode *> que;
que.push(root);
while (!que.empty()) {
TreeNode *temp = que.front();
que.pop();
--len;
cur.push_back(temp->val);
if (temp->left) que.push(temp->left);
if (temp->right) que.push(temp->right);
if (!len) {
ret.push_back(cur);
cur.clear();
len = que.size();
}
}
reverse(ret.begin(), ret.end());
return ret;
}
};