1 """ 2 首先理解题意,因为要找结点的下个指针 3 不能从根节点来看。要从第二层看来判断 4 找到规律自然用递归写法 5 """ 6 class Node: 7 def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None): 8 self.val = val 9 self.left = left 10 self.right = right 11 self.next = next 12 13 class Solution: 14 def connect(self, root: 'Node') -> 'Node': 15 if not root: 16 return None 17 if root.left: 18 root.left.next = root.right 19 if root.right: 20 root.right.next = root.next.left if root.next else None #!!!从第二层来判断 21 self.connect(root.left) 22 self.connect(root.right) 23 return root