-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo107.cc
38 lines (35 loc) · 946 Bytes
/
No107.cc
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
28
29
30
31
32
33
34
35
36
37
38
/**
* Created by Xiaozhong on 2020/8/3.
* Copyright (c) 2020/8/3 Xiaozhong. All rights reserved.
*/
#include "iostream"
#include "vector"
#include "simple_node.h"
#include "queue"
#include "algorithm"
using namespace std;
class Solution {
private:
vector<vector<int>> ans;
queue<TreeNode *> q;
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
if (!root) return ans;
q.push(root);
while (!q.empty()) {
int length = q.size();
vector<int> buffer;
for (int i = 0; i < length; i++) {
TreeNode *node = q.front();
q.pop();
buffer.push_back(node->val);
if (node->left) q.push(node->left);
if (node->right) q.push(node->right);
}
ans.push_back(buffer);
}
// 翻转一下子
reverse(ans.begin(), ans.end());
return ans;
}
};