mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加(116.填充每个节点的下一个右侧节点指针):增加typescript版本
This commit is contained in:
@ -1611,6 +1611,31 @@ var connect = function(root) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
```
|
```
|
||||||
|
TypeScript:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function connect(root: Node | null): Node | null {
|
||||||
|
let helperQueue: Node[] = [];
|
||||||
|
let preNode: Node, curNode: Node;
|
||||||
|
if (root !== null) helperQueue.push(root);
|
||||||
|
while (helperQueue.length > 0) {
|
||||||
|
for (let i = 0, length = helperQueue.length; i < length; i++) {
|
||||||
|
if (i === 0) {
|
||||||
|
preNode = helperQueue.shift()!;
|
||||||
|
} else {
|
||||||
|
curNode = helperQueue.shift()!;
|
||||||
|
preNode.next = curNode;
|
||||||
|
preNode = curNode;
|
||||||
|
}
|
||||||
|
if (preNode.left) helperQueue.push(preNode.left);
|
||||||
|
if (preNode.right) helperQueue.push(preNode.right);
|
||||||
|
}
|
||||||
|
preNode.next = null;
|
||||||
|
}
|
||||||
|
return root;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
go:
|
go:
|
||||||
|
|
||||||
```GO
|
```GO
|
||||||
|
Reference in New Issue
Block a user