-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLeetCode-107.cpp
61 lines (54 loc) · 1.35 KB
/
LeetCode-107.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*************************************************************************
> File Name: LeetCode-107.cpp
> Author:
> Mail:
> Created Time: 2020年02月28日 星期五 17时28分37秒
************************************************************************/
#include<iostream>
using namespace std;
/**
* Definition for a binary tree node.
* 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>> list;
queue<TreeNode*>q;
if(root == nullptr)
return list;
q.push(root);
while(!q.empty())
{
int length = q.size();
vector<int> _list;
for(int i = 0; i < length; ++i)
{
TreeNode* tmp = q.front();
q.pop();
_list.push_back(tmp->val);
if(tmp->left)
{
q.push(tmp->left);
}
if(tmp->right)
{
q.push(tmp->right);
}
}
list.push_back(_list);
}
for(size_t i = 0; i < (list.size() + 1) / 2; i++)
{
vector<int> temp = list[i];
list[i] = list[list.size() - i -1];
list[list.size() - i - 1] = temp;
}
return list;
}
};