117. 填充每个节点的下一个右侧节点指针 II
BFS模板的应用
总结的DFS、BFS模板参考:https://www.cnblogs.com/panweiwei/p/13065661.html
class Solution(object):
def connect(self, root):
"""
:type root: Node
:rtype: Node
"""
if not root:
return root
queue = [root]
while queue:
l = len(queue)
for i in range(l):
node = queue.pop(0)
if i < l - 1:
node.next = queue[0]
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
return root