Print matrix elements from top | Function to traverse the matrix diagonally upwards ; Stores the maximum size of vector from all row of matrix nums [ ] [ ] ; Store elements in desired order ; Store every element on the basis of sum of index ( i + j ) ; Print the stored result ; Reverse all sublist ; Driver code ; Given vector of vectors arr ; Function Call
def printDiagonalTraversal ( nums ) : NEW_LINE INDENT max_size = len ( nums ) NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT if ( max_size < len ( nums [ i ] ) ) : NEW_LINE INDENT max_size = len ( nums [ i ] ) NEW_LINE DEDENT DEDENT v = [ [ ] for i in range ( 2 * max_size - 1 ) ] NEW_LINE for i in range ( len ( nums ) ) : NEW_LINE INDENT for j in range ( len ( nums [ i ] ) ) : NEW_LINE INDENT v [ i + j ] . append ( nums [ i ] [ j ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT v [ i ] = v [ i ] [ : : - 1 ] NEW_LINE for j in range ( len ( v [ i ] ) ) : NEW_LINE INDENT print ( v [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE printDiagonalTraversal ( arr ) NEW_LINE DEDENT