My Solutions
1. Use the BFS\n\n/**\n * Time Complexity: O(n1 * n1 + n2 * n2)\n * Space Complexity: O(n1 + n2)\n * where `n1` is the length of the vector `edges1`\n *       `
**1. Use the BFS**\n```\n/**\n * Time Complexity: O(n1 * n1 + n2 * n2)\n * Space Complexity: O(n1 + n2)\n * where `n1` is the length of the vector `edges1`\n *       `n2` is the length of the vector `edges2`\n */\nclass Solution {\n public:\n  vector<int> maxTargetNodes(const vector<vector<int>> &edges1,\n                             const vector<vector<int>> &edges2,\n                             const int k) {\n    vector<int> ret = get_all_nodes(edges1, k);\n    vector<int> nodes = get_all_nodes(edges2, k - 1);\n    const int max_node = *max_element(nodes.begin(), nodes.end());\n    for (int &item : ret) {\n      item += max_node;\n    }\n    return ret;\n  }\n  \n private:\n  /**\n   * @return: a vector<int> `ret`,\n   *          whose length is the number of the nodes of the tree,\n   *          represented by the vector `edges`,\n   *          each element `ret[i]` of which is the number of the nodes\n   *          whose distance to the node `i` is less than or equal to `distance`\n   */\n  vector<int> get_all_nodes(const vector<vector<int>> &edges, const int distance) {\n    const int n = static_cast<int>(edges.size()) + 1;\n    vector<int> tree[n];\n    for (const vector<int> &edge : edges) {\n      tree[edge.front()].emplace_back(edge.back());\n      tree[edge.back()].emplace_back(edge.front());\n    }\n    \n    vector<int> ret(n);\n    for (int node = 0; node < n; ++node) {\n      ret[node] = get_one_nodes(tree, node, distance);\n    }\n    return ret;\n  }\n  \n  /**\n   * @return: the number of the nodes whose distance to the node `node` is less than\n   *          or equal to `distance`\n   */\n  int get_one_nodes(const vector<int> *tree, const int root, const int distance) {\n    /**\n     * <0>: the node\n     * <1>: the parent node\n     */\n    using q_node_t = array<int, 2>;\n    constexpr int invalid_parent = -1;\n    int ret = 0;\n    queue<q_node_t> q({q_node_t{root, invalid_parent}});\n    for (int d = 0; d < distance + 1 && !q.empty(); ++d) {\n      ret += static_cast<int>(q.size());\n      for (size_t qs = q.size(); qs > 0; --qs) {\n        const auto [node, parent] = q.front();\n        q.pop();\n        for (const int child : tree[node]) {\n          if (child == parent) {\n            continue;\n          }\n          q.emplace(q_node_t{child, node});\n        }\n      }\n    }\n    return ret;\n  }\n};\n```\n**2. Use the DFS**\n```\n/**\n * Time Complexity: O(n1 * n1 + n2 * n2)\n * Space Complexity: O(n1 + n2)\n * where `n1` is the length of the vector `edges1`\n *       `n2` is the length of the vector `edges2`\n */\nclass Solution {\n public:\n  vector<int> maxTargetNodes(const vector<vector<int>> &edges1,\n                             const vector<vector<int>> &edges2,\n                             const int k) {\n    vector<int> ret = get_all_nodes(edges1, k);\n    vector<int> nodes = get_all_nodes(edges2, k - 1);\n    const int max_node = *max_element(nodes.begin(), nodes.end());\n    for (int &item : ret) {\n      item += max_node;\n    }\n    return ret;\n  }\n  \n private:\n  /**\n   * @return: a vector<int> `ret`,\n   *          whose length is the number of the nodes of the tree,\n   *          represented by the vector `edges`,\n   *          each element `ret[i]` of which is the number of the nodes\n   *          whose distance to the node `i` is less than or equal to `distance`\n   */\n  vector<int> get_all_nodes(const vector<vector<int>> &edges, const int distance) {\n    const int n = static_cast<int>(edges.size()) + 1;\n    vector<int> tree[n];\n    for (const vector<int> &edge : edges) {\n      tree[edge.front()].emplace_back(edge.back());\n      tree[edge.back()].emplace_back(edge.front());\n    }\n    \n    vector<int> ret(n);\n    for (int node = 0; node < n; ++node) {\n      ret[node] = get_one_nodes(tree, node, distance);\n    }\n    return ret;\n  }\n  \n  /**\n   * @return: the number of the nodes whose distance to the node `node` is less than\n   *          or equal to `distance`\n   */\n  int get_one_nodes(const vector<int> *tree, const int root, const int distance) {\n    constexpr int invalid_parent = -1;\n    return dfs(tree, root, invalid_parent, 0, distance);\n  }\n  \n  int dfs(const vector<int> *tree,\n          const int node,\n          const int parent,\n          const int current_distance,\n          const int distance) {\n    if (current_distance > distance) {\n      return 0;\n    }\n    \n    int ret = 1;\n    for (const int child : tree[node]) {\n      if (child == parent) {\n        continue;\n      }\n      ret += dfs(tree, child, node, current_distance + 1, distance);\n    }\n    return ret;\n  }\n};\n```
[]