-
Notifications
You must be signed in to change notification settings - Fork 0
/
107.cpp
35 lines (35 loc) · 918 Bytes
/
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
/**
* 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<TreeNode*> v;
vector<vector<int>> res;
if(root == NULL) return res;
v.push_back(root);
while(!v.empty())
{
vector<TreeNode*> tem_v;
vector<int> output;
for(auto it : v)
{
output.push_back(it->val);
if(it->left != NULL)
tem_v.push_back(it->left);
if(it->right != NULL)
tem_v.push_back(it->right);
}
res.push_back(output);
v = tem_v;
}
reverse(res.begin(), res.end());
return res;
}
};