Skip to content

Commit bb5a3f3

Browse files
committedFeb 24, 2022
feat: add ts solution to lc problem: No.0116
No.0116.Populating Next Right Pointers in Each Node
1 parent ecff613 commit bb5a3f3

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
 

‎solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README.md

+36
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,42 @@ public:
195195
};
196196
```
197197
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+
198234
### **...**
199235

200236
```

‎solution/0100-0199/0116.Populating Next Right Pointers in Each Node/README_EN.md

+36
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,42 @@ public:
197197
};
198198
```
199199
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+
200236
### **...**
201237

202238
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.