Fully Explained simple solution in javascript (DFS/Hash)
Please feel free to pitch in. Hopefully this helps someone build an even easier and faster solution.\n\nstep 1: First we find the shortest distance from the roo
Please feel free to pitch in. Hopefully this helps someone build an even easier and faster solution.\n\nstep 1: First we find the shortest distance from the root to the target and keep track of that path as well as the count.  (minPath function takes care of this) \n\nstep 2: We have the shortest path recorded to the target now. So any node that isn\'t a part of that path would be taking 1 step away from the target. Any node that we reach that is on that path is 1 step closer to the target. The node that is the target would be considered a distance of 0. \n\nstep 3: Anytime we reach the K distance, store that value. \n\n```\nroot = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2\n        \n            3\n           /  \\\n          5    1\n        /  \\   | \\\n       6    2  0  8\n           / \\        \n          7   4\n  \nshortest Path = [3,5];  \nThe root has a distance of 1 from the target of 5\n\ndfs: \n\nleft side: \nnode 3 : distance = 1\n|\nnode 5 : distance = 0 ----------------------------\n                    |                            | \nnode 6 : distance = 1                            node 2 : distance = 1\n                                                 |                   |\n                             node 7 : distance = **2**                 node 4 : distance = **2**\nright side: \nnode 3 : distance = 1\n|\nnode 1 : distance = **2**\n\n```\n\n```\nvar distanceK = function(root, target, K) {\n    if (!root) return null;\n\n    let output = []; \n    let distance, minRoute; \n    \n    const minPath = (node, target, total, route) => {\n        route = JSON.parse(JSON.stringify(route));\n        route[node.val] = true;\n        \n        if (node.val === target.val) {\n            distance = total;\n            minRoute = route;\n            return;\n        }    \n        if (node.left) minPath(node.left,target, total + 1, route);\n        if (node.right) minPath(node.right, target, total + 1, route);\n    } \n    \n    minPath(root, target, 0, {});\n    \n    const dfs = (node, dist) => {\n        if (!node) return;\n        \n        if (dist === K) output.push(node.val);\n        \n        if (node.left) {\n            (minRoute[node.left.val]) ? dfs(node.left, dist - 1) : dfs(node.left, dist + 1);\n        }\n        \n        if (node.right) {\n            (minRoute[node.right.val]) ? dfs(node.right, dist - 1) : dfs(node.right, dist + 1);\n        }\n    }\n    \n    dfs(root, distance);\n    return output;\n};\n```
['Depth-First Search']