JavaScript Clean BFS Solution
Approach\n  Convert the binary tree to an undirected graph\n  Do BFS on the graph starting from the target node\n\njavascript\nvar distanceK = function(root, ta
Approach\n*  Convert the binary tree to an undirected graph\n*  Do BFS on the graph starting from the target node\n\n```javascript\nvar distanceK = function(root, target, K) {\n    const adjacencyList = {};\n\n    function createGraph(node, parent) {\n        if(!node) return;\n        const neighbor = [];\n        \n        if(node.left) {\n            neighbor.push(node.left.val);\n            createGraph(node.left, node.val);\n        }\n        if(node.right) {\n            neighbor.push(node.right.val);\n            createGraph(node.right, node.val);\n        }\n        if(parent !== null) neighbor.push(parent);\n        adjacencyList[node.val] = neighbor;\n    }\n    \n    createGraph(root, null);\n    \n    const queue = [[target.val, 0]];\n    const visited = new Set();\n    const result = [];\n    \n    while(queue.length) {\n        const [node, gen] = queue.shift();\n        if(visited.has(node)) continue;\n        if(gen === K) result.push(node);\n        if(gen > K) return result;\n        \n        for(let n of adjacencyList[node]) {\n            queue.push([n, gen+1]);\n        }\n        visited.add(node);\n    }\n    return result;\n};\n```
['Breadth-First Search', 'JavaScript']