304 North Cardinal St.
Dorchester Center, MA 02124

# Reachable Nodes With Restrictions LeetCode Solution

## Problem – Reachable Nodes With Restrictions LeetCode 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 <= 105`
• `edges.length == n - 1`
• `edges[i].length == 2`
• `0 <= ai, bi < n`
• `ai != bi`
• `edges` represents a valid tree.
• `1 <= restricted.length < n`
• `1 <= restricted[i] < n`
• All the values of `restricted` are unique.

### Reachable Nodes With Restrictions LeetCode Solution in Java

``````class Solution {
public int reachableNodes(int n, int[][] edges, int[] restricted) {
List<Integer>[] al = new ArrayList[n];
for(int i=0;i<n;i++) al[i] = new ArrayList<>();
for(int e[] : edges){
}
Set<Integer> set = new HashSet<>();
int ans = 0;
while(!q.isEmpty()){
int size = q.size();
while(size-->0){
ans++;
int curr = q.remove();
for(int next : al[curr])
}
}
return ans;
}
}
``````

### Reachable Nodes With Restrictions LeetCode Solution in C++

``````int reachableNodes(int n, vector<vector<int>>&e , vector<int>& r) {
unordered_set<int> s(begin(r),end(r));
vector<vector<int>> graph(n);
for(auto i:e) graph[i].push_front(i) , graph[i].push_front(i);
queue<int> q;
vector<bool> seen(n);
int cnt=0;
q.push(0);
seen=true;
while(q.size()){
auto node= q.front(); q.pop();
if(s.count(node)) continue;
cnt++;
for(auto j:graph[node]) //Put Neighbours
if(!seen[j]) seen[j]=true , q.push(j);
}
return cnt;
}
``````

### Reachable Nodes With Restrictions LeetCode Solution in Python

``````class Solution:
def reachableNodes(self, n: int, edges: List[List[int]], restricted: List[int]) -> int:
res = set(restricted)
graph = defaultdict(list)
vis = set()

for u, v in edges:
if u in res or v in res: continue
graph[u].append(v)
graph[v].append(u)

def dfs(node):
r = 1
for nei in graph[node]:
if nei in vis: continue
r += dfs(nei)
return r

return dfs(0)
``````
##### Reachable Nodes With Restrictions LeetCode Solution Review:

In our experience, we suggest you solve this Reachable Nodes With Restrictions LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Reachable Nodes With Restrictions LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Reachable Nodes With Restrictions LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions