-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path116.cpp
40 lines (33 loc) · 968 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
29
30
31
32
33
34
35
36
37
38
39
40
// 116. Populating Next Right Pointers in Each Node - https://leetcode.com/problems/populating-next-right-pointers-in-each-node
#include "bits/stdc++.h"
using namespace std;
// 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) {
auto left_start = root;
while (left_start && left_start->left) {
helper(left_start);
left_start = left_start->left;
}
}
void helper(TreeLinkNode* start_node) {
auto cur = start_node;
while (cur) {
cur->left->next = cur->right;
if (cur->next) {
cur->right->next = cur->next->left;
}
cur = cur->next;
}
}
};
int main() {
ios::sync_with_stdio(false);
return 0;
}