From a45fa9dcce962b0906f3c436ddeab7e2c406751a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=BA=A6=E8=8C=97?= <15946078+Zyming2006@user.noreply.gitee.com> Date: Tue, 21 Oct 2025 11:41:14 +0000 Subject: [PATCH 1/2] 20251021 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 曾亦茗 <15946078+Zyming2006@user.noreply.gitee.com> --- ..._\346\233\276\344\272\246\350\214\227.cpp" | 49 +++++++++++++++++++ ..._\346\233\276\344\272\246\350\214\227.cpp" | 26 ++++++++++ 2 files changed, 75 insertions(+) create mode 100644 "topic04/submit/LC310_\346\233\276\344\272\246\350\214\227.cpp" create mode 100644 "topic04/submit/LC687_\346\233\276\344\272\246\350\214\227.cpp" 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 0000000..2edc155 --- /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 0000000..e6f9c11 --- /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 -- Gitee From 0b94ca502687646978e6faaaf1931146a022baf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E4=BA=A6=E8=8C=97?= <15946078+Zyming2006@user.noreply.gitee.com> Date: Tue, 21 Oct 2025 12:39:13 +0000 Subject: [PATCH 2/2] 20251021 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 曾亦茗 <15946078+Zyming2006@user.noreply.gitee.com> --- ..._\346\233\276\344\272\246\350\214\227.cpp" | 21 ++++++++++++++++++ ..._\346\233\276\344\272\246\350\214\227.cpp" | 22 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 "topic04/submit/LC1376_\346\233\276\344\272\246\350\214\227.cpp" create mode 100644 "topic04/submit/LC1443_\346\233\276\344\272\246\350\214\227.cpp" 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 0000000..0388745 --- /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 0000000..c93e1cc --- /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 -- Gitee