-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path116.cpp
28 lines (28 loc) · 795 Bytes
/
116.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
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
// 层次遍历
class Solution {
public:
void connect(TreeLinkNode *root) {
if(root == NULL) return;
vector<TreeLinkNode*> que;
que.push_back(root);
while(!que.empty())
{
vector<TreeLinkNode*> tem;
for(int i = 0; i < que.size(); i++)
{
que[i]->next = i < que.size() - 1 ? que[i+1] : NULL;
if(que[i]->left != NULL) tem.push_back(que[i]->left);
if(que[i]->right != NULL) tem.push_back(que[i]->right);
}
que = tem;
}
}
};