Given a binary tree, Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL. (连接二叉树同一层的结点)
Example:
1. 递归
首先根据根节点的 next 指针找到这个子树的孩子节点的 next 需要连接的位置,然后将孩子节点通过 next 连接起来,再处理子树部分。具体实现过程如下:
""" # Definition for a Node. class Node: def __init__(self, val, left, right, next): self.val = val self.left = left self.right = right self.next = next """ class : defconnect(self, root: 'Node') -> 'Node': ifnot root: return root p = root.next while p: if p.left: p = p.left break if p.right: p = p.right break p = p.next if root.right: root.right.next = p if root.left: if root.right: root.left.next = root.right else: root.left.next = p self.connect(root.right) self.connect(root.left) return root
""" # Definition for a Node. class Node: def __init__(self, val, left, right, next): self.val = val self.left = left self.right = right self.next = next """ class : defconnect(self, root: 'Node') -> 'Node': ifnot root: returnNone queue = [[root]] while queue: top = queue.pop() level_node = [] pre = None for node in top: if node.left: level_node.append(node.left) ifnot pre: pre = node.left else: pre.next = node.left pre = node.left if node.right: level_node.append(node.right) ifnot pre: pre = node.right else: pre.next = node.right pre = node.right if level_node: queue.insert(0, level_node) return root
近期评论