-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-tree-level-order-traversal-ii.cpp
47 lines (46 loc) · 1.37 KB
/
binary-tree-level-order-traversal-ii.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
/**
* 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>> result;
if(!root) return result;
queue<TreeNode*> q1;
queue<TreeNode*> q2;
q1.push(root);
while(!q1.empty() || !q2.empty()){
vector<int> part;
if(!q1.empty()){
while(!q1.empty()){
TreeNode* tmp=q1.front();
q1.pop();
part.push_back(tmp->val);
if(tmp->left) q2.push(tmp->left);
if(tmp->right) q2.push(tmp->right);
}
result.push_back(part);
part.clear();
}
else if(!q2.empty()){
while(!q2.empty()){
TreeNode* tmp=q2.front();
q2.pop();
part.push_back(tmp->val);
if(tmp->left) q1.push(tmp->left);
if(tmp->right) q1.push(tmp->right);
}
result.push_back(part);
part.clear();
}
}
reverse(result.begin(),result.end());
return result;
}
};