Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- int ans = 0;
- vector<int> g[10003];
- int dfs(int u, int fa) {
- int a = -1;
- for (auto v: g[u]) {
- if (v == fa) continue;
- int val = dfs(v, u);
- ans = max(ans, a + 1 + val + 1);
- a = max(a, val);
- }
- a += 1;
- return a;
- }
- public:
- int treeDiameter(vector<vector<int>>& edges) {
- for (const auto& e: edges) g[e[0]].push_back(e[1]), g[e[1]].push_back(e[0]);
- dfs(0, -1);
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement