mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
添加(199.二叉树的右视图):增加typescript版本
This commit is contained in:
@ -709,7 +709,28 @@ var rightSideView = function(root) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function rightSideView(root: TreeNode | null): number[] {
|
||||
let helperQueue: TreeNode[] = [];
|
||||
let resArr: number[] = [];
|
||||
let tempNode: TreeNode;
|
||||
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 === length - 1) resArr.push(tempNode.val);
|
||||
if (tempNode.left !== null) helperQueue.push(tempNode.left);
|
||||
if (tempNode.right !== null) helperQueue.push(tempNode.right);
|
||||
}
|
||||
}
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
func rightSideView(_ root: TreeNode?) -> [Int] {
|
||||
var res = [Int]()
|
||||
|
Reference in New Issue
Block a user