Skip to content

Commit 2a7baf7

Browse files
authored
Populating Next Right Pointers in Each Node
1 parent 8599dd5 commit 2a7baf7

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
13+
if(root==NULL)
14+
return;
15+
16+
if(root->left!=NULL)
17+
root->left->next=root->right;
18+
19+
if(root->next!=NULL && root->right!=NULL)
20+
root->right->next=root->next->left;
21+
22+
connect(root->left);
23+
connect(root->right);
24+
25+
}
26+
};

0 commit comments

Comments
 (0)