添加(111.二叉树的最小深度):增加typescript版本

This commit is contained in:
Steve2020
2022-01-28 20:33:04 +08:00
parent bdb0954e32
commit 626db3fc67

View File

@ -2349,7 +2349,29 @@ var minDepth = function(root) {
};
```
TypeScript:
```typescript
function minDepth(root: TreeNode | null): number {
let helperQueue: TreeNode[] = [];
let resMin: number = 0;
let tempNode: TreeNode;
if (root !== null) helperQueue.push(root);
while (helperQueue.length > 0) {
resMin++;
for (let i = 0, length = helperQueue.length; i < length; i++) {
tempNode = helperQueue.shift()!;
if (tempNode.left === null && tempNode.right === null) return resMin;
if (tempNode.left !== null) helperQueue.push(tempNode.left);
if (tempNode.right !== null) helperQueue.push(tempNode.right);
}
}
return resMin;
};
```
Swift
```swift
func minDepth(_ root: TreeNode?) -> Int {
guard let root = root else {