Convert a Binary Tree into Doubly Linked List in spiral fashion | Binary tree node ; Given a reference to the head of a list and a node , inserts the node on the front of the list . ; Make right of given node as head and left as None ; change left of head node to given node ; move the head to point to the given node ; Function to prints contents of DLL ; Function to prcorner node at each level ; Base Case ; Create an empty deque for doing spiral level order traversal and enqueue root ; create a stack to store Binary Tree nodes to insert into DLL later ; nodeCount indicates number of Nodes at current level . ; Dequeue all Nodes of current level and Enqueue all Nodes of next level odd level ; dequeue node from front & push it to stack ; insert its left and right children in the back of the deque ; even level ; dequeue node from the back & push it to stack ; inserts its right and left children in the front of the deque ; head pointer for DLL ; pop all nodes from stack and push them in the beginning of the list ; Driver Code ; Let us create Binary Tree as shown in above example ; root . right . left . left = newNode ( 12 ) ; root . right . right . right = newNode ( 15 )
class newNode : 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 DEDENT DEDENT def push ( head_ref , node ) : NEW_LINE INDENT node . right = ( head_ref ) NEW_LINE node . left = None NEW_LINE if ( ( head_ref ) != None ) : NEW_LINE INDENT ( head_ref ) . left = node NEW_LINE DEDENT ( head_ref ) = node NEW_LINE DEDENT def printList ( node ) : NEW_LINE INDENT i = 0 NEW_LINE while ( i < len ( node ) ) : NEW_LINE INDENT print ( node [ i ] . data , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def spiralLevelOrder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE stk = [ ] NEW_LINE level = 0 NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT nodeCount = len ( q ) NEW_LINE if ( level & 1 ) : NEW_LINE INDENT while ( nodeCount > 0 ) : NEW_LINE INDENT node = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE stk . append ( node ) NEW_LINE if ( node . left != None ) : NEW_LINE INDENT q . append ( node . left ) NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT q . append ( node . right ) NEW_LINE DEDENT nodeCount -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT while ( nodeCount > 0 ) : NEW_LINE INDENT node = q [ - 1 ] NEW_LINE q . pop ( - 1 ) NEW_LINE stk . append ( node ) NEW_LINE if ( node . right != None ) : NEW_LINE INDENT q . insert ( 0 , node . right ) NEW_LINE DEDENT if ( node . left != None ) : NEW_LINE INDENT q . insert ( 0 , node . left ) NEW_LINE DEDENT nodeCount -= 1 NEW_LINE DEDENT DEDENT level += 1 NEW_LINE DEDENT head = [ ] NEW_LINE while ( len ( stk ) ) : NEW_LINE INDENT head . append ( stk [ 0 ] ) NEW_LINE stk . pop ( 0 ) NEW_LINE DEDENT print ( " Created ▁ DLL ▁ is : " ) NEW_LINE printList ( head ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . left = newNode ( 6 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE root . left . left . left = newNode ( 8 ) NEW_LINE root . left . left . right = newNode ( 9 ) NEW_LINE root . left . right . left = newNode ( 10 ) NEW_LINE root . left . right . right = newNode ( 11 ) NEW_LINE root . right . left . right = newNode ( 13 ) NEW_LINE root . right . right . left = newNode ( 14 ) NEW_LINE spiralLevelOrder ( root ) NEW_LINE DEDENT