題目:
題解:
class Solution:def connect(self, root: 'Node') -> 'Node':if not root:return root# 從根節點開始leftmost = rootwhile leftmost.left:# 遍歷這一層節點組織成的鏈表,為下一層的節點更新 next 指針head = leftmostwhile head:# CONNECTION 1head.left.next = head.right# CONNECTION 2if head.next:head.right.next = head.next.left# 指針向后移動head = head.next# 去下一層的最左的節點leftmost = leftmost.leftreturn root