Skip to content

Commit e4f674c

Browse files
committed
添加(0111.二叉树的最小深度.md):增加typescript版本
1 parent 97bc7ef commit e4f674c

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

problems/0111.二叉树的最小深度.md

+38
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,44 @@ var minDepth = function(root) {
404404
};
405405
```
406406

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+
407445
## Swift
408446

409447
> 递归

0 commit comments

Comments
 (0)