mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加(429.N叉树的层序遍历):增加typescript版本
This commit is contained in:
@ -1190,7 +1190,30 @@ var levelOrder = function(root) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
TypeScript:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function levelOrder(root: Node | null): number[][] {
|
||||||
|
let helperQueue: Node[] = [];
|
||||||
|
let resArr: number[][] = [];
|
||||||
|
let tempArr: number[] = [];
|
||||||
|
if (root !== null) helperQueue.push(root);
|
||||||
|
let curNode: Node;
|
||||||
|
while (helperQueue.length > 0) {
|
||||||
|
for (let i = 0, length = helperQueue.length; i < length; i++) {
|
||||||
|
curNode = helperQueue.shift()!;
|
||||||
|
tempArr.push(curNode.val);
|
||||||
|
helperQueue.push(...curNode.children);
|
||||||
|
}
|
||||||
|
resArr.push(tempArr);
|
||||||
|
tempArr = [];
|
||||||
|
}
|
||||||
|
return resArr;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
Swift:
|
Swift:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func levelOrder(_ root: Node?) -> [[Int]] {
|
func levelOrder(_ root: Node?) -> [[Int]] {
|
||||||
var res = [[Int]]()
|
var res = [[Int]]()
|
||||||
|
Reference in New Issue
Block a user