Try   HackMD
Dark Theme License

The MIT License (MIT)

Copyright © 2022-2023 Lumynous

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

題目連結: Leetcode

題目概要

給一二分樹, 輸出距離目標節點target距離為k的所有節點

想法

DFS + BFS

先DFS將樹建成一張無相圖, 再使用BFS從target作為起點向外尋找距離為k的所有節點

參考解法

歡迎補充

CPP DFS+BFS
class Solution { private: const int MAX_NODES = 500; public: vector<vector<int>> table = vector<vector<int>>(MAX_NODES, vector<int>()); void dfs(TreeNode *node){ if (node == NULL) return; if (node->left != NULL) { table[node->val].push_back(node->left->val); table[node->left->val].push_back(node->val); dfs(node->left); } if (node->right != NULL) { table[node->val].push_back(node->right->val); table[node->right->val].push_back(node->val); dfs(node->right); } } vector<int> distanceK(TreeNode* root, TreeNode* target, int k) { dfs(root); vector<int> ans; queue<pair<int, int>> q; vector<bool> visited(MAX_NODES, false); q.push(make_pair(target->val, 0)); visited[target->val] = true; while (!q.empty()){ int node = q.front().first; int dis = q.front().second; q.pop(); if (dis == k){ ans.push_back(node); continue; } for (int i = 0; i < table[node].size(); i++){ int next = table[node][i]; if (!visited[next]){ visited[next] = true; q.push(make_pair(next, dis + 1)); } } } return ans; } };

Dark Theme License

The MIT License (MIT)

Copyright © 2022 Luminous-Coder

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.