# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# self.next = None
class Solution:
# @param root, a tree node
# @return nothing
def connect(self, root):
fatherLayerCurrent = root
sonLayerHead = None
sonLayerCurrent = None
# Travel layer by layer as BFS travesal.
while fatherLayerCurrent != None:
# Travel inside a layer from left to right
while fatherLayerCurrent != None:
# Try to access the left son if there is
if fatherLayerCurrent.left != None:
if sonLayerHead == None:
sonLayerHead = fatherLayerCurrent.left
sonLayerCurrent = sonLayerHead
else:
sonLayerCurrent.next = fatherLayerCurrent.left
sonLayerCurrent = sonLayerCurrent.next
# Try to access the right son if there is
if fatherLayerCurrent.right != None:
if sonLayerHead == None:
sonLayerHead = fatherLayerCurrent.right
sonLayerCurrent = sonLayerHead
else:
sonLayerCurrent.next = fatherLayerCurrent.right
sonLayerCurrent = sonLayerCurrent.next
fatherLayerCurrent = fatherLayerCurrent.next
# Prepare to move down to the next layer.
fatherLayerCurrent = sonLayerHead
sonLayerHead =None
return