KatInterview

Tree Span and Grid Escape: Two Graph Queries in One Call

Trees & GraphsAuto-graded · 21 test cases
MediumAuto-gradedTrees & Graphsbfstree-diametershortest-path

Last updated

Compute a tree's longest path in edges and the shortest 4-directional route through a grid with obstacles, returning both results together.

You are given two independent graph tasks, and your function must answer both and return the results together.

Part 1: Widest reach of a tree

There is a tree with n nodes labeled 1 through n. It is connected, undirected, and has no cycles. The tree is described by edges, a list of n - 1 pairs [u, v], each meaning an undirected connection between nodes u and v. Every connection counts as length 1.

Find the largest possible distance between any two nodes, where distance is the number of edges on the unique simple path joining them. A tree with a single node has this value 0.

Part 2: Escaping a square grid

There is a square grid with m rows and m columns, where m = len(grid). A cell holding 0 is open and a cell holding 1 is blocked.

You begin at the top-left cell (0, 0) and want to arrive at the bottom-right cell (m - 1, m - 1). In each step you move to one of the four orthogonally adjacent cells (up, down, left, right). You may only enter cells that lie inside the grid and are open.

Find the minimum number of steps needed. Return -1 if the destination cannot be reached, which includes the case where the start cell or the destination cell is blocked. If m = 1 and the single cell is open, the answer is 0.

Output

Return a list of two integers [tree_result, grid_result]:

  • tree_result is the longest distance, in edges, between two nodes of the tree.
  • grid_result is the fewest steps from the top-left to the bottom-right cell, or -1 if it is impossible.

Constraints

  • 1 <= n <= 10^5
  • edges has exactly n - 1 entries and forms a valid tree.
  • The grid is square with side length up to a few hundred; every entry is 0 or 1.
  • Your solution should run in time linear in the size of each input (O(n) for the tree, O(m^2) for the grid).
  • If the grid is empty, treat the grid result as -1.

Examples

Example 1

Input: tree_span_and_grid_escape(5, [[1, 2], [2, 3], [3, 4], [2, 5]], [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]])

Output: [3, 8]

Example 2

Input: tree_span_and_grid_escape(1, [], [[0]])

Output: [0, 0]

Example 3

Input: tree_span_and_grid_escape(4, [[1, 2], [1, 3], [3, 4]], [[0, 1, 0, 0], [0, 1, 0, 1], [0, 0, 0, 1], [1, 1, 0, 0]])

Output: [3, 6]

Hints

Hint 1

For the tree, pick any node and find the node farthest from it. Then think about what happens if you search again from that farthest node.

Hint 2

Every move in the grid costs the same, so a breadth-first search visits cells in order of increasing distance. Mark a cell as seen when you add it to the queue.

Hint 3

Avoid deep recursion on a tree with 100,000 nodes: use an iterative BFS with a queue. Handle the edge cases n = 1 and a blocked start or end cell separately.