mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
添加(515.在每个树行中找最大值):增加typescript版本
This commit is contained in:
@ -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]()
|
||||
|
Reference in New Issue
Block a user