JavaScript BFS solution with comments
\nvar distanceK = function(root, target, K) {\n    let output = []\n    let map = new Map();\n    setParent(root, map);\n\t\n\t//add a set to keep track of your
```\nvar distanceK = function(root, target, K) {\n    let output = []\n    let map = new Map();\n    setParent(root, map);\n\t\n\t//add a set to keep track of your visited nodes\n    let seen = new Set();\n    seen.add(target.val)\n\t\n\t//the distance from target to target is 0 so start at 0 \n    let level = 0;\n\t\n\t//start the bfs with the target node\n    let queue = [target]\n    \n    while (queue.length > 0) {\n       \n        let size = queue.length;\n\t\t\n\t\t//I use a for loop in order to iterate the entire level and at the end of the for loop we add 1 to the level\n        for (let i = 0; i < size; i++) {\n           \n           \n            let node = queue.shift();\n\t\t\t\n             if (level === K) {\n                output.push(node.val)\n                 continue;\n            }\n\t\t\t\n\t\t\t//add the visited node to set\n            seen.add(node.val)\n            \n\t\t\t//add the left node if it isnt null and isnt in the set\n            if (node.left !== null && seen.has(node.left.val) === false) {\n                \n                queue.push(node.left)\n            }\n\t\t\t//add the right node if it isnt null and isnt in the set\n            if (node.right !== null && seen.has(node.right.val) === false) {\n                queue.push(node.right)\n            }\n\t\t\t\n\t\t\t//add the parent if it isnt null and isnt in the set\n            if (map.has(node) && map.get(node) !== null) {\n                if (seen.has(map.get(node).val) === false) {\n                    queue.push(map.get(node))\n                }\n            }\n        }\n        level++\n    }\n    return output;\n    \n};\n\n//setting the hash map with node as key and its parent as values\nconst setParent = (root, map) => {\n    let queue = [[root, null]]\n    while (queue.length > 0) {\n        let nodePair = queue.shift();\n        let node = nodePair[0];\n        let parent = nodePair[1];\n    \n        map.set(node, parent)\n        if (node.left !== null) {\n            queue.push([node.left, node])\n        }\n        if (node.right !== null) {\n            queue.push([node.right, node])\n        }\n    }\n   \n    \n}\n```
[]