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

This commit is contained in:
Steve2020
2022-01-28 17:11:11 +08:00
parent d1abb0cbb4
commit bdb0954e32

View File

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