116. 填充每个节点的下一个右侧节点指针

还是层序遍历,只不过是将每层输出成一个链表
2023.6.12

class Solution:
    def connect(self, root: 'Optional[Node]') -> 'Optional[Node]':
        if not root:
            return root
        mydeque = deque()
        mydeque.append(root)
        while mydeque:
            size = len(mydeque)
            pre = None
            for i in range(size):
                cur = mydeque.popleft()
                if pre:
                    pre.next = cur
                pre = cur
                if cur.left:
                    mydeque.append(cur.left)
                if cur.right:
                    mydeque.append(cur.right)
        return root

你可能感兴趣的:(Notes,of,Leetcode,leetcode)