forked from walkccc/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0107.cpp
More file actions
35 lines (31 loc) · 912 Bytes
/
0107.cpp
File metadata and controls
35 lines (31 loc) · 912 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
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) {
if (!root) return {};
vector<vector<int>> ans;
queue<TreeNode*> queue;
queue.push(root);
while (!queue.empty()) {
vector<int> currLevel;
int currLevelSize = queue.size();
for (int i = 0; i < currLevelSize; i++) {
TreeNode* node = queue.front();
queue.pop();
currLevel.push_back(node->val);
if (node->left) queue.push(node->left);
if (node->right) queue.push(node->right);
}
ans.insert(ans.begin(), currLevel);
}
return ans;
}
};