Python Fastest Queue Solution
\nfrom collections import deque\n\nclass Solution:\n    def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:\n        # construct a BFS tree 
```\nfrom collections import deque\n\nclass Solution:\n    def pacificAtlantic(self, heights: List[List[int]]) -> List[List[int]]:\n        # construct a BFS tree rooted at the pacific ocean and record what nodes we can reach\n        # construct a BFS tree rooted at the atlantic ocean and record what nodes we can reach\n\n        m = len(heights)\n        n = len(heights[0])\n\n        \n        def BFS(grid, startingNodes):\n            # return a list of reachable cells\n            nonlocal m,n\n            \n            Q = deque()\n            Q.extend(startingNodes)\n            visited = set(startingNodes)\n            while Q:\n                r,c = Q.popleft()\n                h = grid[r][c]\n                \n                # north\n                if r>0 and grid[r-1][c]>=h and (r-1,c) not in visited:\n                    visited.add((r-1,c))\n                    Q.append((r-1,c))\n                    \n                # south\n                if r<m-1 and grid[r+1][c]>=h and (r+1,c) not in visited:\n                    visited.add((r+1,c))\n                    Q.append((r+1,c))\n                    \n                # east\n                if c>0 and grid[r][c-1]>=h and (r,c-1) not in visited:\n                    visited.add((r,c-1))\n                    Q.append((r,c-1))\n\n                # east\n                if c<n-1 and grid[r][c+1]>=h and (r,c+1) not in visited:\n                    visited.add((r,c+1))\n                    Q.append((r,c+1))\n                    \n            return visited\n        \n        \n        pacificNeighbours = [(0,0)]\n        for i in range(1,n):\n            pacificNeighbours.append((0,i))\n        for i in range(1,m):\n            pacificNeighbours.append((i,0))\n\n        atlanticNeighbours = [(m-1,n-1)]\n        for i in range(n-1):\n            atlanticNeighbours.append((m-1,i))\n        for i in range(m-1):\n            atlanticNeighbours.append((i,n-1))\n            \n        pCells = BFS(heights, pacificNeighbours)\n        aCells = BFS(heights, atlanticNeighbours)\n        \n        X = pCells.intersection(aCells)\n        return list(X)\n```
['Depth-First Search', 'Recursion', 'Queue']