-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path102.cpp
More file actions
37 lines (37 loc) · 1.06 KB
/
102.cpp
File metadata and controls
37 lines (37 loc) · 1.06 KB
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
//Binary Tree Level Order Traversal
//宽度优先搜索
/**
* 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>> levelOrder(TreeNode* root) {
queue<pair<TreeNode *, int>> que;
vector<vector<int>> ret;
if (root == NULL)
return ret;
int level = 0;
ret.push_back(vector<int>());
que.push(pair<TreeNode*, int>(root,0));
while(!que.empty()) {
auto p = que.front();
if(p.second != level){
level ++;
ret.push_back(vector<int>());
}
ret[level].push_back(p.first->val);
if(p.first->left != NULL)
que.push(pair<TreeNode*, int>(p.first->left, level+1));
if(p.first->right != NULL)
que.push(pair<TreeNode*, int>(p.first->right, level+1));
que.pop();
}
return ret;
}
};