添加(515.在每个树行中找最大值):增加typescript版本

This commit is contained in:
Steve2020
2022-01-28 12:02:15 +08:00
parent 2862d47368
commit 9329ecff5e

View File

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