File tree 9 files changed +251
-2
lines changed
0800-0899/0814.Binary Tree Pruning
2331.Evaluate Boolean Binary Tree
2337.Move Pieces to Obtain a String
9 files changed +251
-2
lines changed Original file line number Diff line number Diff line change @@ -185,6 +185,36 @@ var pruneTree = function (root) {
185
185
};
186
186
```
187
187
188
+ ### ** TypeScript**
189
+
190
+ ``` ts
191
+ /**
192
+ * Definition for a binary tree node.
193
+ * class TreeNode {
194
+ * val: number
195
+ * left: TreeNode | null
196
+ * right: TreeNode | null
197
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
198
+ * this.val = (val===undefined ? 0 : val)
199
+ * this.left = (left===undefined ? null : left)
200
+ * this.right = (right===undefined ? null : right)
201
+ * }
202
+ * }
203
+ */
204
+
205
+ function pruneTree(root : TreeNode | null ): TreeNode | null {
206
+ if (root == null ) {
207
+ return root ;
208
+ }
209
+ root .left = pruneTree (root .left );
210
+ root .right = pruneTree (root .right );
211
+ if (root .val == 0 && root .left == null && root .right == null ) {
212
+ return null ;
213
+ }
214
+ return root ;
215
+ }
216
+ ```
217
+
188
218
### ** ...**
189
219
190
220
```
Original file line number Diff line number Diff line change @@ -174,6 +174,36 @@ var pruneTree = function (root) {
174
174
};
175
175
```
176
176
177
+ ### ** TypeScript**
178
+
179
+ ``` ts
180
+ /**
181
+ * Definition for a binary tree node.
182
+ * class TreeNode {
183
+ * val: number
184
+ * left: TreeNode | null
185
+ * right: TreeNode | null
186
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
187
+ * this.val = (val===undefined ? 0 : val)
188
+ * this.left = (left===undefined ? null : left)
189
+ * this.right = (right===undefined ? null : right)
190
+ * }
191
+ * }
192
+ */
193
+
194
+ function pruneTree(root : TreeNode | null ): TreeNode | null {
195
+ if (root == null ) {
196
+ return root ;
197
+ }
198
+ root .left = pruneTree (root .left );
199
+ root .right = pruneTree (root .right );
200
+ if (root .val == 0 && root .left == null && root .right == null ) {
201
+ return null ;
202
+ }
203
+ return root ;
204
+ }
205
+ ```
206
+
177
207
### ** ...**
178
208
179
209
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * class TreeNode {
4
+ * val: number
5
+ * left: TreeNode | null
6
+ * right: TreeNode | null
7
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.left = (left===undefined ? null : left)
10
+ * this.right = (right===undefined ? null : right)
11
+ * }
12
+ * }
13
+ */
14
+
15
+ function pruneTree ( root : TreeNode | null ) : TreeNode | null {
16
+ if ( root == null ) {
17
+ return root ;
18
+ }
19
+ root . left = pruneTree ( root . left ) ;
20
+ root . right = pruneTree ( root . right ) ;
21
+ if ( root . val == 0 && root . left == null && root . right == null ) {
22
+ return null ;
23
+ }
24
+ return root ;
25
+ }
Original file line number Diff line number Diff line change @@ -185,7 +185,30 @@ func evaluateTree(root *TreeNode) bool {
185
185
### ** TypeScript**
186
186
187
187
``` ts
188
+ /**
189
+ * Definition for a binary tree node.
190
+ * class TreeNode {
191
+ * val: number
192
+ * left: TreeNode | null
193
+ * right: TreeNode | null
194
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
195
+ * this.val = (val===undefined ? 0 : val)
196
+ * this.left = (left===undefined ? null : left)
197
+ * this.right = (right===undefined ? null : right)
198
+ * }
199
+ * }
200
+ */
188
201
202
+ function evaluateTree(root : TreeNode | null ): boolean {
203
+ const { val, left, right } = root ;
204
+ if (left == null && right == null ) {
205
+ return !! val ;
206
+ }
207
+ if (val === 2 ) {
208
+ return evaluateTree (left ) || evaluateTree (right );
209
+ }
210
+ return evaluateTree (left ) && evaluateTree (right );
211
+ }
189
212
```
190
213
191
214
### ** ...**
Original file line number Diff line number Diff line change @@ -173,7 +173,30 @@ func evaluateTree(root *TreeNode) bool {
173
173
### ** TypeScript**
174
174
175
175
``` ts
176
+ /**
177
+ * Definition for a binary tree node.
178
+ * class TreeNode {
179
+ * val: number
180
+ * left: TreeNode | null
181
+ * right: TreeNode | null
182
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
183
+ * this.val = (val===undefined ? 0 : val)
184
+ * this.left = (left===undefined ? null : left)
185
+ * this.right = (right===undefined ? null : right)
186
+ * }
187
+ * }
188
+ */
176
189
190
+ function evaluateTree(root : TreeNode | null ): boolean {
191
+ const { val, left, right } = root ;
192
+ if (left == null && right == null ) {
193
+ return !! val ;
194
+ }
195
+ if (val === 2 ) {
196
+ return evaluateTree (left ) || evaluateTree (right );
197
+ }
198
+ return evaluateTree (left ) && evaluateTree (right );
199
+ }
177
200
```
178
201
179
202
### ** ...**
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * class TreeNode {
4
+ * val: number
5
+ * left: TreeNode | null
6
+ * right: TreeNode | null
7
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.left = (left===undefined ? null : left)
10
+ * this.right = (right===undefined ? null : right)
11
+ * }
12
+ * }
13
+ */
14
+
15
+ function evaluateTree ( root : TreeNode | null ) : boolean {
16
+ const { val, left, right } = root ;
17
+ if ( left == null && right == null ) {
18
+ return ! ! val ;
19
+ }
20
+ if ( val === 2 ) {
21
+ return evaluateTree ( left ) || evaluateTree ( right ) ;
22
+ }
23
+ return evaluateTree ( left ) && evaluateTree ( right ) ;
24
+ }
Original file line number Diff line number Diff line change @@ -231,7 +231,38 @@ func canChange(start string, target string) bool {
231
231
### ** TypeScript**
232
232
233
233
``` ts
234
-
234
+ function canChange(start : string , target : string ): boolean {
235
+ if (
236
+ [... start ].filter (c => c !== ' _' ).join (' ' ) !==
237
+ [... target ].filter (c => c !== ' _' ).join (' ' )
238
+ ) {
239
+ return false ;
240
+ }
241
+ const n = start .length ;
242
+ let i = 0 ;
243
+ let j = 0 ;
244
+ while (i < n || j < n ) {
245
+ while (start [i ] === ' _' ) {
246
+ i ++ ;
247
+ }
248
+ while (target [j ] === ' _' ) {
249
+ j ++ ;
250
+ }
251
+ if (start [i ] === ' R' ) {
252
+ if (i > j ) {
253
+ return false ;
254
+ }
255
+ }
256
+ if (start [i ] === ' L' ) {
257
+ if (i < j ) {
258
+ return false ;
259
+ }
260
+ }
261
+ i ++ ;
262
+ j ++ ;
263
+ }
264
+ return true ;
265
+ }
235
266
```
236
267
237
268
### ** ...**
Original file line number Diff line number Diff line change @@ -224,7 +224,38 @@ func canChange(start string, target string) bool {
224
224
### ** TypeScript**
225
225
226
226
``` ts
227
-
227
+ function canChange(start : string , target : string ): boolean {
228
+ if (
229
+ [... start ].filter (c => c !== ' _' ).join (' ' ) !==
230
+ [... target ].filter (c => c !== ' _' ).join (' ' )
231
+ ) {
232
+ return false ;
233
+ }
234
+ const n = start .length ;
235
+ let i = 0 ;
236
+ let j = 0 ;
237
+ while (i < n || j < n ) {
238
+ while (start [i ] === ' _' ) {
239
+ i ++ ;
240
+ }
241
+ while (target [j ] === ' _' ) {
242
+ j ++ ;
243
+ }
244
+ if (start [i ] === ' R' ) {
245
+ if (i > j ) {
246
+ return false ;
247
+ }
248
+ }
249
+ if (start [i ] === ' L' ) {
250
+ if (i < j ) {
251
+ return false ;
252
+ }
253
+ }
254
+ i ++ ;
255
+ j ++ ;
256
+ }
257
+ return true ;
258
+ }
228
259
```
229
260
230
261
### ** ...**
Original file line number Diff line number Diff line change
1
+ function canChange ( start : string , target : string ) : boolean {
2
+ if (
3
+ [ ...start ] . filter ( c => c !== '_' ) . join ( '' ) !==
4
+ [ ...target ] . filter ( c => c !== '_' ) . join ( '' )
5
+ ) {
6
+ return false ;
7
+ }
8
+ const n = start . length ;
9
+ let i = 0 ;
10
+ let j = 0 ;
11
+ while ( i < n || j < n ) {
12
+ while ( start [ i ] === '_' ) {
13
+ i ++ ;
14
+ }
15
+ while ( target [ j ] === '_' ) {
16
+ j ++ ;
17
+ }
18
+ if ( start [ i ] === 'R' ) {
19
+ if ( i > j ) {
20
+ return false ;
21
+ }
22
+ }
23
+ if ( start [ i ] === 'L' ) {
24
+ if ( i < j ) {
25
+ return false ;
26
+ }
27
+ }
28
+ i ++ ;
29
+ j ++ ;
30
+ }
31
+ return true ;
32
+ }
You can’t perform that action at this time.
0 commit comments