Construct a complete binary tree from given array in level order fashion | Helper function that allocates a new node ; Function to insert nodes in level order ; Base case for recursion ; insert left child ; insert right child ; Function to print tree nodes in InOrder fashion ; Driver Code
class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def insertLevelOrder ( arr , root , i , n ) : NEW_LINE INDENT if i < n : NEW_LINE INDENT temp = newNode ( arr [ i ] ) NEW_LINE root = temp NEW_LINE root . left = insertLevelOrder ( arr , root . left , 2 * i + 1 , n ) NEW_LINE root . right = insertLevelOrder ( arr , root . right , 2 * i + 2 , n ) NEW_LINE DEDENT return root NEW_LINE DEDENT def inOrder ( root ) : NEW_LINE INDENT if root != None : NEW_LINE INDENT inOrder ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE inOrder ( root . right ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 6 , 6 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE root = None NEW_LINE root = insertLevelOrder ( arr , root , 0 , n ) NEW_LINE inOrder ( root ) NEW_LINE DEDENT