Watch 10 video solutions for Reachable Nodes With Restrictions, a medium level problem involving Array, Hash Table, Tree. This walkthrough by Code with Alisha has 1,588 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
There is an undirected tree with n nodes labeled from 0 to n - 1 and n - 1 edges.
You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree. You are also given an integer array restricted which represents restricted nodes.
Return the maximum number of nodes you can reach from node 0 without visiting a restricted node.
Note that node 0 will not be a restricted node.
Example 1:
Input: n = 7, edges = [[0,1],[1,2],[3,1],[4,0],[0,5],[5,6]], restricted = [4,5] Output: 4 Explanation: The diagram above shows the tree. We have that [0,1,2,3] are the only nodes that can be reached from node 0 without visiting a restricted node.
Example 2:
Input: n = 7, edges = [[0,1],[0,2],[0,5],[0,4],[3,2],[6,5]], restricted = [4,2,1] Output: 3 Explanation: The diagram above shows the tree. We have that [0,5,6] are the only nodes that can be reached from node 0 without visiting a restricted node.
Constraints:
2 <= n <= 105edges.length == n - 1edges[i].length == 20 <= ai, bi < nai != biedges represents a valid tree.1 <= restricted.length < n1 <= restricted[i] < nrestricted are unique.Problem Overview: You are given an undirected tree with n nodes labeled from 0 to n-1. Starting from node 0, count how many nodes you can visit without stepping into any node listed in the restricted array. Once a restricted node is encountered, traversal must stop before entering it.
The structure is a tree, so there is exactly one path between any two nodes. The main task is to traverse the graph while skipping restricted nodes and avoiding revisits. A set for fast restriction lookup and an adjacency list for the graph representation make the traversal efficient.
Approach 1: DFS Using Adjacency List (Time: O(n), Space: O(n))
Build an adjacency list from the edge list to represent the tree. Store all restricted nodes in a hash set for O(1) membership checks. Start a Depth‑First Search from node 0, and recursively explore neighbors that are not restricted and not yet visited. Each node is processed once, and each edge is examined at most twice. DFS works well because trees have no cycles other than the parent link, so a visited set prevents backtracking loops.
This approach is concise and natural for recursive traversal. During DFS, increment a counter whenever a valid node is visited. The recursion stops immediately if the next node appears in the restricted set. This solution uses the classic depth-first search technique on a graph representation.
Approach 2: BFS Using Adjacency List (Time: O(n), Space: O(n))
Instead of recursion, perform a level‑order traversal using a queue. Initialize the queue with node 0 and repeatedly pop nodes while pushing valid neighbors. Before adding a neighbor, check two conditions: it must not be restricted and must not have been visited before. Each node enters the queue at most once, which keeps the runtime linear.
BFS processes nodes level by level, which makes it easy to reason about traversal order and avoids recursion stack limits. The adjacency list still provides efficient neighbor iteration. This approach applies the standard breadth-first search pattern used for many reachability problems.
Recommended for interviews: Either DFS or BFS with an adjacency list is the expected solution. Both run in O(n) time and demonstrate that you understand graph traversal and constraint filtering. DFS is slightly shorter to implement, while BFS avoids recursion and is sometimes preferred in production systems. Showing both approaches demonstrates strong understanding of graph traversal fundamentals.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| DFS Using Adjacency List | O(n) | O(n) | Best general approach for tree traversal. Concise implementation and common in coding interviews. |
| BFS Using Adjacency List | O(n) | O(n) | Useful when avoiding recursion or when level-by-level traversal is preferred. |