-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.py
38 lines (35 loc) · 1.13 KB
/
connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File : connect.py
@Time : 2024/10/09 21:10:36
@Author : Zihao Zheng
@Email : [email protected]
'''
class Node:
def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
self.val = val
self.left = left
self.right = right
self.next = next
from typing import List
from collections import deque
class Solution():
def connect(self,root):
if not root:
return []
queue=deque([root])
result=[]
while queue:
level=[]
prev_node=None
for _ in range(len(queue)):
cur=queue.popleft()
if prev_node: #如果该节点存在前一个节点,则将该节点变成前一个节点的next 否则自己变成别人的前一个节点
prev_node.next=cur
prev_node=cur
if cur.left:
queue.append(cur.left)
if cur.right:
queue.append(cur.right)
return root