mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +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:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func rightSideView(_ root: TreeNode?) -> [Int] {
|
func rightSideView(_ root: TreeNode?) -> [Int] {
|
||||||
var res = [Int]()
|
var res = [Int]()
|
||||||
|
Reference in New Issue
Block a user