File tree 2 files changed +43
-0
lines changed
0116.Populating Next Right Pointers in Each Node
0117.Populating Next Right Pointers in Each Node II
2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ Node* connect (Node* root) {
4
+ if (!root) return nullptr ;
5
+ queue<Node*> q;
6
+ q.push (root);
7
+ while (!q.empty ()) {
8
+ int size = q.size ();
9
+ for (int i = 0 ; i < size; ++i) {
10
+ Node* t = q.front ();
11
+ q.pop ();
12
+ if (i < size - 1 ) {
13
+ t->next = q.front ();
14
+ }
15
+ if (t->left ) q.push (t->left );
16
+ if (t->right ) q.push (t->right );
17
+ }
18
+ }
19
+ return root;
20
+ }
21
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ Node* connect (Node* root) {
4
+ if (!root) return nullptr ;
5
+ queue<Node*> q;
6
+ q.push (root);
7
+ while (!q.empty ()) {
8
+ int size = q.size ();
9
+ for (int i = 0 ; i < size; ++i) {
10
+ Node* t = q.front ();
11
+ q.pop ();
12
+ if (i < size - 1 ) {
13
+ t->next = q.front ();
14
+ }
15
+ if (t->left ) q.push (t->left );
16
+ if (t->right ) q.push (t->right );
17
+ }
18
+ }
19
+
20
+ return root;
21
+ }
22
+ };
You can’t perform that action at this time.
0 commit comments