Treating it as a binary tree level order traversal extension !


  • 0
    D
    class Solution(object):
        def findDiagonalOrder(self, matrix):
            """
            :type matrix: List[List[int]]
            :rtype: List[int]
            """
            if matrix == []:
                return []
        
            level = 0
            queue = [(0,0)]
            child = []
        
            M = len(matrix)
            N = len(matrix[0])
            ans = []
            while queue:
                for i in queue:
                    r = i[0]
                    c = i[1]
                    if r+1 < M and (r+1,c) not in child:
                        child.append((r+1,c))
                    if c+1 < N and (r,c+1) not in child:
                        child.append((r,c+1))
                level += 1
                t = map(lambda x:matrix[x[0]][x[1]],queue)
                if level % 2 == 0:
                    ans += t[::-1]
                else:
                    ans += t
                queue = child[:]
                child = []
            return ans

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.