-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0107.cpp
45 lines (41 loc) · 1.01 KB
/
0107.cpp
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
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
#include <deque>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>> ans;
deque<TreeNode*> que;
if (root != NULL)
que.push_back(root);
else
return ans;
while (!que.empty())
{
int size = que.size();
vector<int> temp;
for (int i = 0; i < size; i++)
{
TreeNode* node = que.front();
temp.push_back(node->val);
que.pop_front();
if (node->left != NULL)
que.push_back(node->left);
if (node->right != NULL)
que.push_back(node->right);
}ans.push_back(temp);
}
reverse(ans.begin(), ans.end());
return ans;
}
};