mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2026-02-02 18:39:09 +08:00
Update
This commit is contained in:
@@ -2073,26 +2073,6 @@ class Solution:
|
||||
if curnode.right: queue.append(curnode.right)
|
||||
return root
|
||||
|
||||
# 链表解法
|
||||
class Solution:
|
||||
def connect(self, root: 'Node') -> 'Node':
|
||||
if not root:
|
||||
return None
|
||||
first = root
|
||||
while first: # 遍历每一层
|
||||
dummyHead = Node(None) # 为下一行创建一个虚拟头节点,相当于下一行所有节点链表的头结点(每一层都会创建);
|
||||
tail = dummyHead # 为下一行维护一个尾节点指针(初始化是虚拟节点)
|
||||
cur = first
|
||||
while cur: # 遍历当前层的节点
|
||||
if cur.left: # 链接下一行的节点
|
||||
tail.next = cur.left
|
||||
tail = tail.next
|
||||
if cur.right:
|
||||
tail.next = cur.right
|
||||
tail = tail.next
|
||||
cur = cur.next # cur同层移动到下一节点
|
||||
first = dummyHead.next # 此处为换行操作,更新到下一行
|
||||
return root
|
||||
```
|
||||
JavaScript:
|
||||
```javascript
|
||||
|
||||
Reference in New Issue
Block a user