Zigzag ( or diagonal ) traversal of Matrix |  ; we will use a 2D vector to store the diagonals of our array the 2D vector will have ( n + m - 1 ) rows that is equal to the number of diagnols ; Driver Code ; Function call
R = 5 NEW_LINE C = 5 NEW_LINE def diagonalOrder ( arr , n , m ) : NEW_LINE INDENT ans = [ [ ] for i in range ( n + m - 1 ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT ans [ i + j ] . append ( arr [ j ] [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( ans ) ) : NEW_LINE INDENT for j in range ( len ( ans [ i ] ) ) : NEW_LINE INDENT print ( ans [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE m = 4 NEW_LINE arr = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] , [ 17 , 18 , 19 , 20 ] ] NEW_LINE diagonalOrder ( arr , n , m ) NEW_LINE