Easy || c++ || BFS solution 🔥🔥🔥
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:O(n^2*log(n))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n\n\n```C++ []\nclass Solution {\npublic:\n    int n;\n    void convertBinaryTreeToUndirectedGraph(TreeNode* root, TreeNode* parent, unordered_map<TreeNode*, vector<TreeNode*>>& graph) {\n        if (root == nullptr) {\n            return;\n        }\n\n        if (parent) {\n            n++;\n            graph[root].push_back(parent);\n            graph[parent].push_back(root);\n        }\n\n        convertBinaryTreeToUndirectedGraph(root->left, root, graph);\n        convertBinaryTreeToUndirectedGraph(root->right, root, graph);\n    }\n\n    vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {\n        vector<int> result;\n        if (root == nullptr || target == nullptr) {\n            return result;\n        }\n\n        unordered_map<TreeNode*, vector<TreeNode*>> graph;\n        convertBinaryTreeToUndirectedGraph(root, nullptr, graph);\n\n        queue<TreeNode*> q;\n        unordered_set<TreeNode*> visited;\n        map<TreeNode*,int>distance;\n        q.push(target);\n        visited.insert(target);\n        distance[target] = 0;\n\n        // int distance = 0;\n\n        while (!q.empty()) {\n            TreeNode* root = q.front();\n            q.pop();\n            if(distance[root] == k){\n                result.push_back(root->val);\n            }\n            for(TreeNode * root1 : graph[root]){\n                if(visited.find(root1) == visited.end()){\n                    visited.insert(root1);\n                    distance[root1] = distance[root] + 1;\n                    q.push(root1);\n                }\n            }\n        }\n\n        return result;\n    }\n};\n\n```\n```Python []\nfrom collections import deque, defaultdict\n\nclass TreeNode:\n    def __init__(self, x):\n        self.val = x\n        self.left = None\n        self.right = None\n\nclass Solution:\n    def __init__(self):\n        self.n = 0\n\n    def convertBinaryTreeToUndirectedGraph(self, root, parent, graph):\n        if root is None:\n            return\n\n        if parent:\n            self.n += 1\n            graph[root].append(parent)\n            graph[parent].append(root)\n\n        self.convertBinaryTreeToUndirectedGraph(root.left, root, graph)\n        self.convertBinaryTreeToUndirectedGraph(root.right, root, graph)\n\n    def distanceK(self, root, target, k):\n        result = []\n        if root is None or target is None:\n            return result\n\n        graph = defaultdict(list)\n        self.convertBinaryTreeToUndirectedGraph(root, None, graph)\n\n        q = deque([target])\n        visited = set([target])\n        distance = {target: 0}\n\n        while q:\n            node = q.popleft()\n            if distance[node] == k:\n                result.append(node.val)\n            for neighbor in graph[node]:\n                if neighbor not in visited:\n                    visited.add(neighbor)\n                    distance[neighbor] = distance[node] + 1\n                    q.append(neighbor)\n\n        return result\n```\n\n```Java []\nimport java.util.*;\n\nclass TreeNode {\n    int val;\n    TreeNode left;\n    TreeNode right;\n    TreeNode(int x) { val = x; }\n}\n\npublic class Solution {\n    int n = 0;\n\n    public void convertBinaryTreeToUndirectedGraph(TreeNode root, TreeNode parent, Map<TreeNode, List<TreeNode>> graph) {\n        if (root == null) {\n            return;\n        }\n\n        if (parent != null) {\n            n++;\n            graph.computeIfAbsent(root, k -> new ArrayList<>()).add(parent);\n            graph.computeIfAbsent(parent, k -> new ArrayList<>()).add(root);\n        }\n\n        convertBinaryTreeToUndirectedGraph(root.left, root, graph);\n        convertBinaryTreeToUndirectedGraph(root.right, root, graph);\n    }\n\n    public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {\n        List<Integer> result = new ArrayList<>();\n        if (root == null || target == null) {\n            return result;\n        }\n\n        Map<TreeNode, List<TreeNode>> graph = new HashMap<>();\n        convertBinaryTreeToUndirectedGraph(root, null, graph);\n\n        Queue<TreeNode> q = new LinkedList<>();\n        Set<TreeNode> visited = new HashSet<>();\n        Map<TreeNode, Integer> distance = new HashMap<>();\n        q.add(target);\n        visited.add(target);\n        distance.put(target, 0);\n\n        while (!q.isEmpty()) {\n            TreeNode node = q.poll();\n            if (distance.get(node) == k) {\n                result.add(node.val);\n            }\n            if (graph.containsKey(node)) {\n                for (TreeNode neighbor : graph.get(node)) {\n                    if (!visited.contains(neighbor)) {\n                        visited.add(neighbor);\n                        distance.put(neighbor, distance.get(node) + 1);\n                        q.add(neighbor);\n                    }\n                }\n            }\n        }\n\n        return result;\n    }\n\n    public static void main(String[] args) {\n        // Example usage\n        TreeNode root = new TreeNode(3);\n        root.left = new TreeNode(5);\n        root.right = new TreeNode(1);\n        root.left.left = new TreeNode(6);\n        root.left.right = new TreeNode(2);\n        root.right.left = new TreeNode(0);\n        root.right.right = new TreeNode(8);\n        root.left.right.left = new TreeNode(7);\n        root.left.right.right = new TreeNode(4);\n\n        Solution solution = new Solution();\n        List<Integer> result = solution.distanceK(root, root.left, 2);\n        System.out.println(result); // Output should be [7, 4, 1]\n    }\n}\n```\n
['C++']