【题目】
【代码】
"""
# Definition for a Node.
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
"""
class Solution:
def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
if root==None:return root
queue=[root]
while queue:
sz=len(queue)
for i in range(len(queue)):
temp=queue.pop(0)
if i<sz-1:
temp.next=queue[0]
if temp.left:
queue.append(temp.left)
if temp.right:
queue.append(temp.right)
return root