Binary search + dfs O(N*logn) || Beats 100%
Intuition
To find minimum possible value of the maximum edge weight ,we should do binary search on answer and check whether with the obtained weight using binar
# Intuition
1. To find minimum possible value of the maximum edge weight ,we should do binary search on answer and check whether with the obtained weight using binary search can reach to 0 from all nodes
2. If yes, then make answer as mid and high=mid-1 and again apply binary search 
3. If No, the low=mid+1
4. For the checking whether it is possible to reach 0 from all nodes to make it linear
5. Reverse the edges 
6. Using dfs and maintaing visited array check whether it is possible to vist all nodes from 0
7. If yes return true
8. Else false

# Complexity Analysis
- Time complexity:
O(N*logN) : O(logn) for binary search on weight and o(n) for dfs and check function

- Space complexity:
O(N): For visited vector 

# Code
```cpp []
class Solution {
public:
    // possible to reach from all nodes to 0
    void dfs(int src,vector<pair<int,int>>adj[],vector<int>&vis,int mid){
       vis[src]=1;
       for(auto it:adj[src]){
          if(!vis[it.first] && it.second<=mid){
            dfs(it.first,adj,vis,mid);
          }
       }
    }
    
    // check function to find possiblity
    bool check(int src,vector<pair<int,int>>adj[],int mid,int n){

        vector<int>vis(n,0);
        dfs(src,adj,vis,mid);

        for(int i=0;i<vis.size();i++){
            if(vis[i]==0){
                return false;
            }
        }
        return true;
    }

    int minMaxWeight(int n, vector<vector<int>>& edges, int threshold) {
        
        vector<pair<int,int>>adj[n];
        int mini=INT_MAX,maxi=INT_MIN;
        
        //Reversing the edges
        for(auto it:edges){
            adj[it[1]].push_back({it[0],it[2]});
            mini=min(mini,it[2]);
            maxi=max(maxi,it[2]);
        }

        int ans=-1;
        int low=mini,high=maxi;

        //Binary search on answer
        while(low<=high){
            int mid=low+(high-low)/2;

            if(check(0,adj,mid,n)){
                ans=mid;
                high=mid-1;
            }
            else{
                low=mid+1;
            }
        }
        return ans;
    }
};
```
```java []
import java.util.*;

class Solution {
    private void dfs(int src, List<List<int[]>> adj, boolean[] vis, int mid) {
        vis[src] = true;
        for (int[] neighbor : adj.get(src)) {
            if (!vis[neighbor[0]] && neighbor[1] <= mid) {
                dfs(neighbor[0], adj, vis, mid);
            }
        }
    }

    private boolean check(int src, List<List<int[]>> adj, int mid, int n) {
        boolean[] vis = new boolean[n];
        dfs(src, adj, vis, mid);

        for (boolean v : vis) {
            if (!v) return false;
        }
        return true;
    }

    public int minMaxWeight(int n, int[][] edges, int threshold) {
        List<List<int[]>> adj = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            adj.add(new ArrayList<>());
        }

        int mini = Integer.MAX_VALUE, maxi = Integer.MIN_VALUE;

        for (int[] edge : edges) {
            adj.get(edge[1]).add(new int[] { edge[0], edge[2] });
            mini = Math.min(mini, edge[2]);
            maxi = Math.max(maxi, edge[2]);
        }

        int ans = -1, low = mini, high = maxi;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (check(0, adj, mid, n)) {
                ans = mid;
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }

        return ans;
    }
}

```
```python []
class Solution:
    def dfs(self, src, adj, vis, mid):
        vis[src] = True
        for neighbor, weight in adj[src]:
            if not vis[neighbor] and weight <= mid:
                self.dfs(neighbor, adj, vis, mid)
    
    def check(self, src, adj, mid, n):
        vis = [False] * n
        self.dfs(src, adj, vis, mid)
        return all(vis)
    
    def minMaxWeight(self, n, edges, threshold):
        adj = [[] for _ in range(n)]
        mini, maxi = float('inf'), float('-inf')

        for x, y, w in edges:
            adj[y].append((x, w))
            mini = min(mini, w)
            maxi = max(maxi, w)
        
        ans = -1
        low, high = mini, maxi

        while low <= high:
            mid = low + (high - low) // 2
            if self.check(0, adj, mid, n):
                ans = mid
                high = mid - 1
            else:
                low = mid + 1
        
        return ans

```
```javascript []
class Solution {
    dfs(src, adj, vis, mid) {
        vis[src] = true;
        for (const [neighbor, weight] of adj[src]) {
            if (!vis[neighbor] && weight <= mid) {
                this.dfs(neighbor, adj, vis, mid);
            }
        }
    }

    check(src, adj, mid, n) {
        const vis = new Array(n).fill(false);
        this.dfs(src, adj, vis, mid);
        return vis.every(v => v);
    }

    minMaxWeight(n, edges, threshold) {
        const adj = Array.from({ length: n }, () => []);
        let mini = Infinity, maxi = -Infinity;

        for (const [x, y, w] of edges) {
            adj[y].push([x, w]);
            mini = Math.min(mini, w);
            maxi = Math.max(maxi, w);
        }

        let ans = -1, low = mini, high = maxi;

        while (low <= high) {
            const mid = Math.floor(low + (high - low) / 2);
            if (this.check(0, adj, mid, n)) {
                ans = mid;
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }

        return ans;
    }
}
```
# If like the solution upvote
['Hash Table', 'Binary Search', 'Greedy', 'Depth-First Search', 'Breadth-First Search', 'Graph', 'Python', 'C++', 'Java', 'JavaScript']