Perfect Binary Tree Specific Level Order Traversal | A binary tree ndoe ; Given a perfect binary tree print its node in specific order ; Let us print root and next level first ; Since it is perfect Binary tree , one of the node is needed to be checked ; Do anythong more if there are nodes at next level in given perfect Binary Tree ; Create a queue and enqueue left and right children of root ; We process two nodes at a time , so we need two variables to stroe two front items of queue ; Traversal loop ; Pop two items from queue ; Print children of first and second in reverse order ; If first and second have grandchildren , enqueue them in reverse order ; Driver program to test above function Perfect Binary Tree of Height 4
class Node : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def printSpecificLevelOrder ( root ) : NEW_LINE INDENT if root is None : NEW_LINE INDENT return NEW_LINE DEDENT print root . data , NEW_LINE if root . left is not None : NEW_LINE INDENT print root . left . data , NEW_LINE print root . right . data , NEW_LINE DEDENT if root . left . left is None : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root . left ) NEW_LINE q . append ( root . right ) NEW_LINE first = None NEW_LINE second = None NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT first = q . pop ( 0 ) NEW_LINE second = q . pop ( 0 ) NEW_LINE print first . left . data , NEW_LINE print second . right . data , NEW_LINE print first . right . data , NEW_LINE print second . left . data , NEW_LINE if first . left . left is not None : NEW_LINE INDENT q . append ( first . left ) NEW_LINE q . append ( second . right ) NEW_LINE q . append ( first . right ) NEW_LINE q . append ( second . left ) NEW_LINE DEDENT DEDENT DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 6 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . left . left . left = Node ( 8 ) NEW_LINE root . left . left . right = Node ( 9 ) NEW_LINE root . left . right . left = Node ( 10 ) NEW_LINE root . left . right . right = Node ( 11 ) NEW_LINE root . right . left . left = Node ( 12 ) NEW_LINE root . right . left . right = Node ( 13 ) NEW_LINE root . right . right . left = Node ( 14 ) NEW_LINE root . right . right . right = Node ( 15 ) NEW_LINE root . left . left . left . left = Node ( 16 ) NEW_LINE root . left . left . left . right = Node ( 17 ) NEW_LINE root . left . left . right . left = Node ( 18 ) NEW_LINE root . left . left . right . right = Node ( 19 ) NEW_LINE root . left . right . left . left = Node ( 20 ) NEW_LINE root . left . right . left . right = Node ( 21 ) NEW_LINE root . left . right . right . left = Node ( 22 ) NEW_LINE root . left . right . right . right = Node ( 23 ) NEW_LINE root . right . left . left . left = Node ( 24 ) NEW_LINE root . right . left . left . right = Node ( 25 ) NEW_LINE root . right . left . right . left = Node ( 26 ) NEW_LINE root . right . left . right . right = Node ( 27 ) NEW_LINE root . right . right . left . left = Node ( 28 ) NEW_LINE root . right . right . left . right = Node ( 29 ) NEW_LINE root . right . right . right . left = Node ( 30 ) NEW_LINE root . right . right . right . right = Node ( 31 ) NEW_LINE print " Specific ▁ Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is " NEW_LINE printSpecificLevelOrder ( root ) ; NEW_LINE