Connect Nodes at same Level ( Level Order Traversal ) | connect nodes at same level using level order traversal ; Node class ; set nextRight of all nodes of a tree ; null marker to represent end of current level ; do level order of tree using None markers ; next element in queue represents next node at current level ; pus left and right children of current node ; if queue is not empty , push NULL to mark nodes at this level are visited ; Driver program to test above functions . ; Constructed binary tree is 10 / \ 8 2 / \ 3 90 ; Populates nextRight pointer in all nodes ; Let us check the values of nextRight pointers ; print level by level ; print inorder
import sys NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE self . nextRight = None NEW_LINE DEDENT def __str__ ( self ) : NEW_LINE INDENT return ' { } ' . format ( self . data ) NEW_LINE DEDENT DEDENT def connect ( root ) : NEW_LINE INDENT queue = [ ] NEW_LINE queue . append ( root ) NEW_LINE queue . append ( None ) NEW_LINE while queue : NEW_LINE INDENT p = queue . pop ( 0 ) NEW_LINE if p : NEW_LINE INDENT p . nextRight = queue [ 0 ] NEW_LINE if p . left : NEW_LINE INDENT queue . append ( p . left ) NEW_LINE DEDENT if p . right : NEW_LINE INDENT queue . append ( p . right ) NEW_LINE DEDENT DEDENT elif queue : NEW_LINE INDENT queue . append ( None ) NEW_LINE DEDENT DEDENT DEDENT def main ( ) : NEW_LINE INDENT root = Node ( 10 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 2 ) NEW_LINE root . left . left = Node ( 3 ) NEW_LINE root . right . right = Node ( 90 ) NEW_LINE connect ( root ) NEW_LINE print ( " Following are populated nextRight pointers in " 	 " the tree ( - 1 is printed if there is no nextRight ) " ) NEW_LINE if ( root . nextRight != None ) : NEW_LINE INDENT print ( " nextRight of % d is % d " % ( root . data , root . nextRight . data ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " nextRight of % d is % d " % ( root . data , - 1 ) ) NEW_LINE DEDENT if ( root . left . nextRight != None ) : NEW_LINE INDENT print ( " nextRight of % d is % d " % ( root . left . data , root . left . nextRight . data ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " nextRight of % d is % d " % ( root . left . data , - 1 ) ) NEW_LINE DEDENT if ( root . right . nextRight != None ) : NEW_LINE INDENT print ( " nextRight of % d is % d " % ( root . right . data , root . right . nextRight . data ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " nextRight of % d is % d " % ( root . right . data , - 1 ) ) NEW_LINE DEDENT if ( root . left . left . nextRight != None ) : NEW_LINE INDENT print ( " nextRight of % d is % d " % ( root . left . left . data , root . left . left . nextRight . data ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " nextRight of % d is % d " % ( root . left . left . data , - 1 ) ) NEW_LINE DEDENT if ( root . right . right . nextRight != None ) : NEW_LINE INDENT print ( " nextRight of % d is % d " % ( root . right . right . data , root . right . right . nextRight . data ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " nextRight of % d is % d " % ( root . right . right . data , - 1 ) ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT def printLevelByLevel ( root ) : NEW_LINE INDENT if root : NEW_LINE INDENT node = root NEW_LINE while node : NEW_LINE INDENT print ( ' { } ' . format ( node . data ) , end = ' ▁ ' ) NEW_LINE node = node . nextRight NEW_LINE DEDENT print ( ) NEW_LINE if root . left : NEW_LINE INDENT printLevelByLevel ( root . left ) NEW_LINE DEDENT else : NEW_LINE INDENT printLevelByLevel ( root . right ) NEW_LINE DEDENT DEDENT DEDENT def inorder ( root ) : NEW_LINE INDENT if root : NEW_LINE INDENT inorder ( root . left ) NEW_LINE print ( root . data , end = ' ▁ ' ) NEW_LINE inorder ( root . right ) NEW_LINE DEDENT DEDENT