8000 2 · javasharper/leetcode@405b552 · GitHub
[go: up one dir, main page]

Skip to content

Commit 405b552

Browse files
committed
2
1 parent ba7ac03 commit 405b552

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for binary tree with next pointer.
3+
* struct TreeLinkNode {
4+
* int val;
5+
* TreeLinkNode *left, *right, *next;
6+
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
void connect(TreeLinkNode *root) {
12+
// Start typing your C/C++ solution below
13+
// DO NOT write int main() function
14+
15+
TreeLinkNode *link_head = root;
16+
TreeLinkNode *next_link_head = NULL;
17+
18+
while (link_head) {
19+
if (link_head->left == NULL)
20+
break;
21+
next_link_head = link_head->left;
22+
next_link_head->next = link_head->right;
23+
TreeLinkNode *link_node = link_head;
24+
TreeLinkNode *next_link_node = link_head->right;
25+
while (link_node->next) {
26+
next_link_node->next = link_node->next->left;
27+
next_link_node->next->next = link_node->next->right;
28+
next_link_node = link_node->next->right;
29+
link_node = link_node->next;
30+
}
31+
link_head = next_link_head;
32+
}
33+
}
34+
};

0 commit comments

Comments
 (0)
0