Easy Graph 🕸 solution || explanatory comments || Beats 94.55% in runtime || Beats 90.45% in memory
Intuition\n\nThe intuition behind the solution is to create a graph of all the nodes in the binary tree, where each node is connected to its parent and its chil
## Intuition\n\nThe intuition behind the solution is to create a graph of all the nodes in the binary tree, where each node is connected to its parent and its children. This allows us to easily traverse the graph and find all the nodes that are at a distance of `k` from the target node.\n\n## Approach\n\nThe approach is as follows:\n\n1. Create a graph of all the nodes in the binary tree.\n2. Traverse the graph and find all the nodes that are at a distance of `k` from the target node.\n3. Return the node values of all the nodes found in the previous step.\n\n## TC & SC\n\nThe time complexity of the solution is $$O(N)$$, where `N` is the number of nodes in the binary tree. The space complexity of the solution is $$O(N)$$, where `N` is the number of nodes in the graph.\n\n![image.png](https://assets.leetcode.com/users/images/ff3694ae-9b11-4096-8f3c-2f7509db6fbf_1689083227.7707486.png)\n\n\n\n# Javascript Code\n```javascript\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n *     this.val = val;\n *     this.left = this.right = null;\n * }\n */\n/**\n * @param {TreeNode} root\n * @param {TreeNode} target\n * @param {number} k\n * @return {number[]}\n */\nvar distanceK = function (root, target, k) {\n    if (!root) return null;\n    if (k === 0) return [target.val]\n    // create a new graph\n    const graph = new Map();\n    graph.set(root.val, [])\n    // create the bi-directional connections via traversing the tree\n    preOrder(root, graph)\n    // console.log({ graph })\n\n    const elemsWithDistK = [];\n    const visited = new Set();\n    // traverse the graph and collect the node values with k distance from target\n    graphTraversal(graph, target.val, k, visited, elemsWithDistK)\n    // get only distinct node values\n    const tempSet = new Set(elemsWithDistK)\n\n    return [...tempSet];\n};\n\nfunction graphTraversal(graph, root, k, visited, elemsWithDistK) {\n    if (k < 0 || visited.has(root)) return;\n\n    // console.log({ root, k, visited, elemsWithDistK })\n\n    if (k === 0) {\n        elemsWithDistK.push(root);\n        return;\n    }\n\n    visited.add(root)\n\n    for (let node of graph.get(root)) {\n        graphTraversal(graph, node, k - 1, visited, elemsWithDistK)\n    }\n}\n\nfunction preOrder(root, graph) {\n    if (!root) return;\n\n    if (root.left) {\n        graph.get(root.val).push(root.left.val);\n        graph.set(root.left.val, [root.val]);\n    }\n    if (root.right) {\n        graph.get(root.val).push(root.right.val);\n        graph.set(root.right.val, [root.val]);\n    }\n\n\n    preOrder(root.left, graph);\n    preOrder(root.right, graph);\n}\n```\n\n---\n\n# Typescript Code\n```typescript\n\n/**\n * Definition for a binary tree node.\n * class TreeNode {\n *     val: number\n *     left: TreeNode | null\n *     right: TreeNode | null\n *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {\n *         this.val = (val===undefined ? 0 : val)\n *         this.left = (left===undefined ? null : left)\n *         this.right = (right===undefined ? null : right)\n *     }\n * }\n */\n\nfunction distanceK(root: TreeNode | null, target: TreeNode | null, k: number): number[] {\n    if (!root) return null;\n    if (k === 0) return [target.val]\n    // create a new graph\n    const graph = new Map<number, number[]>();\n    graph.set(root.val, [])\n    // create the bi-directional connections via traversing the tree\n    preOrder(root, graph)\n\n    const elemsWithDistK: Array<number> = [];\n    const visited = new Set<number>();\n    // traverse the graph and collect the node values with k distance from target\n    graphTraversal(graph, target.val, k, visited, elemsWithDistK)\n    // get only distinct node values\n    const tempSet = new Set(elemsWithDistK)\n\n    return [...tempSet];\n};\n\nfunction graphTraversal(graph: Map<number, number[]>, root: number, k: number, visited: Set<number>, elemsWithDistK: Array<number>): undefined {\n    if (k < 0 || visited.has(root)) return;\n\n\n    if (k === 0) {\n        elemsWithDistK.push(root);\n        return;\n    }\n\n    visited.add(root)\n\n    for (let node of graph.get(root)) {\n        graphTraversal(graph, node, k - 1, visited, elemsWithDistK)\n    }\n}\n\nfunction preOrder(root: TreeNode, graph: Map<number, number[]>): void {\n    if (!root) return;\n\n    if (root.left) {\n        graph.get(root.val).push(root.left.val);\n        graph.set(root.left.val, [root.val]);\n    }\n    if (root.right) {\n        graph.get(root.val).push(root.right.val);\n        graph.set(root.right.val, [root.val]);\n    }\n\n\n    preOrder(root.left, graph);\n    preOrder(root.right, graph);\n}\n\n```
['Graph', 'TypeScript', 'JavaScript']