-
Notifications
You must be signed in to change notification settings - Fork 0
/
56_jianzhi_printree.cpp
46 lines (43 loc) · 1.2 KB
/
56_jianzhi_printree.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
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<int> v;
vector<vector<int>> vec;
if(pRoot == NULL)
return vec;
queue<TreeNode*> q;
q.push(pRoot);
int bedelete = 1, nextlevel = 0;
while(!q.empty()){
TreeNode* node = q.front();
bedelete--;
q.pop();
if(node->left != NULL){
q.push(node->left);
nextlevel++;
}
if(node->right != NULL){
q.push(node->right);
nextlevel++;
}
v.push_back(node->val);
if(bedelete == 0){
vec.push_back(v);
v.clear();
bedelete = nextlevel;
nextlevel=0;
}
}
return vec;
}
};