Python Sorted Trees + BFS
The problem dictates the path of ascending tree path (unique heights of tree). Hence we just need to find steps between current_tree and next_tree in the sorted
The problem dictates the path of ascending tree path (unique heights of tree). Hence we just need to find steps between current_tree and next_tree in the sorted trees. Summing up all should give us the total steps. To find path between tree and next_tree we could use any search(dfs/bfs) but bfs would be optimal. If no path exists between any two consequitve nodes in the sorted trees, it implies no solution exist.  \n\n```\nclass Solution:\n        \n    def pathExists(self, src, dst, forest):\n        \n        rows = len(forest)\n        cols = len(forest[0])\n        \n        dst_x = dst[1]\n        dst_y = dst[2]\n        \n        def bfs(q, visited):\n            while (q):\n                (x,y, count) = q.popleft()\n                \n                if (x,y) == (dst_x, dst_y):\n                    return count\n\n                dx, dy = 0, 1   \n                for i in range(4):\n                    nx = x + dx\n                    ny = y + dy\n                    if (0 <= nx < rows) and (0 <= ny < cols) and (forest[nx][ny] >= 1) and (nx, ny) not in visited:\n                        visited.add((nx, ny))\n                        q.append((nx, ny, count + 1))\n                    dx, dy = -dy, dx\n                \n            return -1\n            \n        v, x, y = src\n        visited = {(x,y)}\n        q = collections.deque()\n        q.append((x,y,0))\n        return bfs(q, visited)\n    \n    def cutOffTree(self, forest: List[List[int]]) -> int:\n        \n        rows = len(forest)\n        cols = len(forest[0])\n        trees = sorted([(forest[i][j], i,j)  for i in range(rows) for j in range(cols) if forest[i][j] > 1])\n        \n        start = (forest[0][0], 0 ,0)\n        mn_steps = 0\n        for next_tree in trees:\n            steps = self.pathExists(start, next_tree, forest)\n            start = next_tree\n            forest[start[1]][start[2]] = 1\n            if steps < 0:\n                return -1\n            else:\n                mn_steps += steps\n        \n        return mn_steps\n```
[]