File tree 3 files changed +103
-0
lines changed
solution/0100-0199/0116.Populating Next Right Pointers in Each Node
3 files changed +103
-0
lines changed Original file line number Diff line number Diff line change @@ -195,6 +195,42 @@ public:
195
195
};
196
196
```
197
197
198
+ ### **TypeScript**
199
+
200
+ ```ts
201
+ /**
202
+ * Definition for Node.
203
+ * class Node {
204
+ * val: number
205
+ * left: Node | null
206
+ * right: Node | null
207
+ * next: Node | null
208
+ * constructor(val?: number, left?: Node, right?: Node, next?: Node) {
209
+ * this.val = (val===undefined ? 0 : val)
210
+ * this.left = (left===undefined ? null : left)
211
+ * this.right = (right===undefined ? null : right)
212
+ * this.next = (next===undefined ? null : next)
213
+ * }
214
+ * }
215
+ */
216
+
217
+ function connect(root: Node | null): Node | null {
218
+ if (root == null) {
219
+ return root;
220
+ }
221
+ const { left, right } = root;
222
+ if (left != null) {
223
+ left.next = right;
224
+ }
225
+ if (right != null && root.next != null) {
226
+ root.right.next = root.next.left;
227
+ }
228
+ connect(root.left);
229
+ connect(root.right);
230
+ return root;
231
+ }
232
+ ```
233
+
198
234
### ** ...**
199
235
200
236
```
Original file line number Diff line number Diff line change @@ -197,6 +197,42 @@ public:
197
197
};
198
198
```
199
199
200
+ ### **TypeScript**
201
+
202
+ ```ts
203
+ /**
204
+ * Definition for Node.
205
+ * class Node {
206
+ * val: number
207
+ * left: Node | null
208
+ * right: Node | null
209
+ * next: Node | null
210
+ * constructor(val?: number, left?: Node, right?: Node, next?: Node) {
211
+ * this.val = (val===undefined ? 0 : val)
212
+ * this.left = (left===undefined ? null : left)
213
+ * this.right = (right===undefined ? null : right)
214
+ * this.next = (next===undefined ? null : next)
215
+ * }
216
+ * }
217
+ */
218
+
219
+ function connect(root: Node | null): Node | null {
220
+ if (root == null) {
221
+ return root;
222
+ }
223
+ const { left, right } = root;
224
+ if (left != null) {
225
+ left.next = right;
226
+ }
227
+ if (right != null && root.next != null) {
228
+ root.right.next = root.next.left;
229
+ }
230
+ connect(root.left);
231
+ connect(root.right);
232
+ return root;
233
+ }
234
+ ```
235
+
200
236
### ** ...**
201
237
202
238
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for Node.
3
+ * class Node {
4
+ * val: number
5
+ * left: Node | null
6
+ * right: Node | null
7
+ * next: Node | null
8
+ * constructor(val?: number, left?: Node, right?: Node, next?: Node) {
9
+ * this.val = (val===undefined ? 0 : val)
10
+ * this.left = (left===undefined ? null : left)
11
+ * this.right = (right===undefined ? null : right)
12
+ * this.next = (next===undefined ? null : next)
13
+ * }
14
+ * }
15
+ */
16
+
17
+ function connect ( root : Node | null ) : Node | null {
18
+ if ( root == null ) {
19
+ return root ;
20
+ }
21
+ const { left, right } = root ;
22
+ if ( left != null ) {
23
+ left . next = right ;
24
+ }
25
+ if ( right != null && root . next != null ) {
26
+ root . right . next = root . next . left ;
27
+ }
28
+ connect ( root . left ) ;
29
+ connect ( root . right ) ;
30
+ return root ;
31
+ }
You can’t perform that action at this time.
0 commit comments