File tree 1 file changed +38
-0
lines changed
1 file changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -404,6 +404,44 @@ var minDepth = function(root) {
404
404
};
405
405
```
406
406
407
+ ## TypeScript
408
+
409
+ > 递归法
410
+
411
+ ```typescript
412
+ function minDepth (root: TreeNode | null): number {
413
+ if (root === null) return 0;
414
+ if (root.left !== null && root.right === null) {
415
+ return 1 + minDepth(root.left);
416
+ }
417
+ if (root.left === null && root.right !== null) {
418
+ return 1 + minDepth(root.right);
419
+ }
420
+ return 1 + Math.min(minDepth(root.left), minDepth(root.right));
421
+ }
422
+ ```
423
+
424
+ > 迭代法
425
+
426
+ ```typescript
427
+ function minDepth(root: TreeNode | null): number {
428
+ let helperQueue: TreeNode[] = [];
429
+ let resMin: number = 0;
430
+ let tempNode: TreeNode;
431
+ if (root !== null) helperQueue.push(root);
432
+ while (helperQueue.length > 0) {
433
+ resMin++;
434
+ for (let i = 0, length = helperQueue.length; i < length; i++) {
435
+ tempNode = helperQueue.shift()!;
436
+ if (tempNode.left === null && tempNode.right === null) return resMin;
437
+ if (tempNode.left !== null) helperQueue.push(tempNode.left);
438
+ if (tempNode.right !== null) helperQueue.push(tempNode.right);
439
+ }
440
+ }
441
+ return resMin;
442
+ };
443
+ ```
444
+
407
445
## Swift
408
446
409
447
> 递归
You can’t perform that action at this time.
0 commit comments