Print Left View of a Binary Tree | Utility function to create a new tree node ; function to print left view of binary tree ; number of nodes at current level ; Traverse all nodes of current level ; Print the left most element at the level ; Add left node to queue ; Add right node to queue ; Driver Code ; construct binary tree as shown in above diagram
class newNode : 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 self . hd = 0 NEW_LINE DEDENT DEDENT def printRightView ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT n = len ( q ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( i == 1 ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE DEDENT if ( temp . left != None ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right != None ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 10 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 7 ) NEW_LINE root . left . right = newNode ( 8 ) NEW_LINE root . right . right = newNode ( 15 ) NEW_LINE root . right . left = newNode ( 12 ) NEW_LINE root . right . right . left = newNode ( 14 ) NEW_LINE printRightView ( root ) NEW_LINE DEDENT