File tree Expand file tree Collapse file tree 2 files changed +52
-9
lines changed
0116_populating_next_right_pointers_in_each_node Expand file tree Collapse file tree 2 files changed +52
-9
lines changed Original file line number Diff line number Diff line change 11#include <stdio.h>
22#include <stdlib.h>
33
4- struct TreeLinkNode {
4+
5+ struct Node {
56 int val ;
6- struct TreeLinkNode * left ;
7- struct TreeLinkNode * right ;
8- struct TreeLinkNode * next ;
7+ struct Node * left ;
8+ struct Node * right ;
9+ struct Node * next ;
910};
1011
11- static void connect (struct TreeLinkNode * root )
12+ struct Node * connect (struct Node * root )
1213{
1314 if (root == NULL ) {
14- return ;
15+ return root ;
1516 }
1617
17- struct TreeLinkNode * head = root ;
18+ struct Node * head = root ;
1819 while (head -> left != NULL ) {
19- struct TreeLinkNode * p ;
20+ struct Node * p ;
2021 for (p = head ; p != NULL ; p = p -> next ) {
2122 p -> left -> next = p -> right ;
2223 p -> right -> next = p -> next == NULL ? NULL : p -> next -> left ;
2324 }
2425 head = head -> left ;
2526 }
27+ return root ;
2628}
2729
2830int main (int argc , char * * argv )
2931{
30- struct TreeLinkNode root , n1 [2 ], n2 [4 ], n3 [8 ];
32+ struct Node root , n1 [2 ], n2 [4 ], n3 [8 ];
3133 root .val = 5 ;
3234 n1 [0 ].val = 4 ;
3335 n1 [1 ].val = 8 ;
Original file line number Diff line number Diff line change 1+ #include < stdc++.h>
2+
3+ using namespace std ;
4+
5+ /*
6+ // Definition for a Node.
7+ class Node {
8+ public:
9+ int val;
10+ Node* left;
11+ Node* right;
12+ Node* next;
13+
14+ Node() : val(0), left(NULL), right(NULL), next(NULL) {}
15+
16+ Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}
17+
18+ Node(int _val, Node* _left, Node* _right, Node* _next)
19+ : val(_val), left(_left), right(_right), next(_next) {}
20+ };
21+ */
22+
23+ class Solution {
24+ public:
25+ Node* connect (Node* root) {
26+ if (root == nullptr ) {
27+ return root;
28+ }
29+
30+ if (root->left != nullptr ) {
31+ root->left ->next = root->right ;
32+ }
33+ Node *next = root->next ;
34+ if (root->right != nullptr && next != nullptr ) {
35+ root->right ->next = next->left ;
36+ }
37+ connect (root->left );
38+ connect (root->right );
39+ return root;
40+ }
41+ };
You can’t perform that action at this time.
0 commit comments