mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加(104.二叉树的最大深度):增加typescript版本
This commit is contained in:
@ -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 {
|
||||
|
Reference in New Issue
Block a user