-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-tree-level-order-traversal-ii.cpp
49 lines (47 loc) · 1.19 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
48
49
/**
* 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) {
queue<TreeNode*> q;
vector<int> v;
vector< vector<int> > v1;
q.push(root);
q.push(NULL);
if(root==NULL)
return v1;
while(!q.empty())
{
TreeNode* temp = q.front();
q.pop();
if(temp==NULL)
{
v1.push_back(v);
//for(int a:v)
// cout<<a;
// cout<<endl;
v.clear();
if(q.empty())
break;
q.push(NULL);
//cout<<"new level";
continue;
}
v.push_back(temp->val);
//cout<<temp->val;
if(temp->left!=NULL )
q.push(temp->left);
if(temp->right!=NULL)
q.push(temp->right);
}
reverse(v1.begin(), v1.end());
return v1;
}
};