forked from gh877916059/LintCode
/
070. 二叉树的层次遍历 II.cpp
40 lines (40 loc) · 1000 Bytes
/
070. 二叉树的层次遍历 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
class Solution
{
public:
vector<vector<int>> levelOrderBottom(TreeNode *root)
{
vector<vector<int> > result;
vector<int> cur;
if(root==NULL)
return result;
queue<TreeNode*> Q;
int counter1=1;
int counter2=0;
Q.push(root);
while(!Q.empty())
{
while(counter1-->0)
{
TreeNode* temp=Q.front();
cur.push_back(temp->val);
Q.pop();
if(temp->left!=NULL)
{
Q.push(temp->left);
++counter2;
}
if(temp->right!=NULL)
{
Q.push(temp->right);
++counter2;
}
}
result.push_back(cur);
cur.clear();
counter1=counter2;
counter2=0;
}
reverse(result.begin(),result.end());
return result;
}
};