mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加(111.二叉树的最小深度):增加typescript版本
This commit is contained in:
@ -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:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func minDepth(_ root: TreeNode?) -> Int {
|
func minDepth(_ root: TreeNode?) -> Int {
|
||||||
guard let root = root else {
|
guard let root = root else {
|
||||||
|
Reference in New Issue
Block a user