-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path23.从上往下打印二叉树.cpp
More file actions
35 lines (32 loc) · 899 Bytes
/
23.从上往下打印二叉树.cpp
File metadata and controls
35 lines (32 loc) · 899 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
/*
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<vector<int> > res;
if(pRoot == NULL) return res;
deque<TreeNode*> q;
q.push_back(pRoot);
while(!q.empty()){
int n = q.size();
vector<int> level;
while(n--){
TreeNode *p = q.front();
q.pop_front();
level.push_back(p->val);
if(p->left) q.push_back(p->left);
if(p->right) q.push_back(p->right);
}
res.push_back(level);
}
return res;
}
};