diff --git "a/topic04/submit/LC1376_\346\233\276\344\272\246\350\214\227.cpp" "b/topic04/submit/LC1376_\346\233\276\344\272\246\350\214\227.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..038874584283298ac88824991fb2cfe0dfaf5e7a --- /dev/null +++ "b/topic04/submit/LC1376_\346\233\276\344\272\246\350\214\227.cpp" @@ -0,0 +1,21 @@ +class Solution { +public: + int numOfMinutes(int n, int headID, vector& manager, vector& informTime) { + vector>subordinates(n); + for(int i=0;i>& subordinates,const vector& informTime){ + int maxTime = 0; + for(int sub:subordinates[node]){ + maxTime = max(maxTime,dfs(sub,subordinates,informTime)); + } + return informTime[node]+maxTime; + } +}; \ No newline at end of file diff --git "a/topic04/submit/LC1443_\346\233\276\344\272\246\350\214\227.cpp" "b/topic04/submit/LC1443_\346\233\276\344\272\246\350\214\227.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c93e1ccbb7a8ebdbaf08550158b4ad2bab7f1538 --- /dev/null +++ "b/topic04/submit/LC1443_\346\233\276\344\272\246\350\214\227.cpp" @@ -0,0 +1,22 @@ +class Solution { +public: + int minTime(int n, vector>& edges, vector& hasApple) { + vector> adj(n); + for (auto& e : edges) { + adj[e[0]].push_back(e[1]); + adj[e[1]].push_back(e[0]); + } + function dfs = [&](int node, int parent) { + int time = 0; + for (int child : adj[node]) { + if (child == parent) continue; + int childTime = dfs(child, node); + if (childTime > 0 || hasApple[child]) { + time += childTime + 2; + } + } + return time; + }; + return dfs(0, -1); + } +}; \ No newline at end of file diff --git "a/topic04/submit/LC310_\346\233\276\344\272\246\350\214\227.cpp" "b/topic04/submit/LC310_\346\233\276\344\272\246\350\214\227.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..2edc15534f0b359676342455d9e0f3d7783cbfa8 --- /dev/null +++ "b/topic04/submit/LC310_\346\233\276\344\272\246\350\214\227.cpp" @@ -0,0 +1,49 @@ +#include +#include +#include +using namespace std; + +class Solution { +public: + vector findMinHeightTrees(int n, vector>& edges) { + if (n == 1) return {0}; + + vector> adj(n); + for (auto& e : edges) { + adj[e[0]].insert(e[1]); + adj[e[1]].insert(e[0]); + } + + queue q; + for (int i = 0; i < n; ++i) { + if (adj[i].size() == 1) { + q.push(i); + } + } + + int remaining = n; + while (remaining > 2) { + int size = q.size(); + remaining -= size; + + for (int i = 0; i < size; ++i) { + int leaf = q.front(); + q.pop(); + + int neighbor = *adj[leaf].begin(); + adj[neighbor].erase(leaf); + + if (adj[neighbor].size() == 1) { + q.push(neighbor); + } + } + } + + vector res; + while (!q.empty()) { + res.push_back(q.front()); + q.pop(); + } + return res; + } +}; \ No newline at end of file diff --git "a/topic04/submit/LC687_\346\233\276\344\272\246\350\214\227.cpp" "b/topic04/submit/LC687_\346\233\276\344\272\246\350\214\227.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..e6f9c11620f308aa7931e665706405164a5acff8 --- /dev/null +++ "b/topic04/submit/LC687_\346\233\276\344\272\246\350\214\227.cpp" @@ -0,0 +1,26 @@ +#include +using namespace std; + +class Solution { +private: + int max_len = 0; + + int dfs(TreeNode* root) { + if (!root) return 0; + + int left = dfs(root->left); + int right = dfs(root->right); + + int left_contribution = (root->left && root->left->val == root->val) ? left + 1 : 0; + int right_contribution = (root->right && root->right->val == root->val) ? right + 1 : 0; + + max_len = max(max_len, left_contribution + right_contribution); + return max(left_contribution, right_contribution); + } + +public: + int longestUnivaluePath(TreeNode* root) { + dfs(root); + return max_len; + } +}; \ No newline at end of file