Very simple Python BFS,  But Why TLE??
I thought it is a straightforward BFS search, so I write it like the following. \nActually, I have the same question with Number of Island problem:\nhttps://dis
I thought it is a straightforward BFS search, so I write it like the following. \nActually, I have the same question with `Number of Island` problem:\nhttps://discuss.leetcode.com/topic/88586/why-python-bfs-get-time-exceed-error\n\n\n```\nimport collections\nclass Solution(object):\n    def cutOffTree(self, G):\n        """\n        :type forest: List[List[int]]\n        :rtype: int\n        """\n        if not G or not G[0]: return -1\n        m, n = len(G), len(G[0])\n        trees = []\n        for i in xrange(m):\n            for j in xrange(n):\n                if G[i][j] > 1:\n                    trees.append((G[i][j], i, j))\n        trees = sorted(trees)\n        count = 0\n        cx, cy = 0, 0\n        for h, x, y in trees:\n            step = self.BFS(G, cx, cy, x, y)\n            if step == -1:\n                return -1\n            else:\n                count += step\n                G[x][y] = 1\n                cx, cy = x, y\n        return count\n\n    def BFS(self, G, cx, cy, tx, ty):\n        m, n = len(G), len(G[0])\n        visited = [[False for j in xrange(n)] for i in xrange(m)]\n        Q = collections.deque()\n        step = -1\n        Q.append((cx, cy))\n        while len(Q) > 0:\n            size = len(Q)\n            step += 1\n            for i in xrange(size):\n                x, y = Q.popleft()\n                visited[x][y] = True\n                if x == tx and y == ty:\n                    return step\n                for nx, ny in [(x + 1, y), (x - 1, y), (x, y-1), (x, y + 1)]:\n                    if nx < 0 or nx >= m or ny < 0 or ny >= n or G[nx][ny] == 0 or visited[nx][ny]:\n                        continue\n                    Q.append((nx, ny))\n        return -1\n```
[]