题目:

题解:
class Solution:
    def connect(self, root: 'Node') -> 'Node':
        if not root:
            return None
        start = root
        while start:
            self.last = None
            self.nextStart = None
            p = start
            while p:
                if p.left:
                    self.handle(p.left)
                if p.right:
                    self.handle(p.right)
                p = p.next
            start = self.nextStart
        return root
    def handle(self, p):
        if self.last:
            self.last.next = p
        if not self.nextStart:
            self.nextStart = p
        self.last = p














![[书生·浦语大模型实战营]——在茴香豆 Web 版中创建自己领域的知识问答助手](https://img-blog.csdnimg.cn/direct/20f83b9661ae450c9737d1c320030803.png)


