TypeScript/JavaScript O(N) time and space
\nfunction distanceK(  root: TreeNode | null,  target: TreeNode | null,  k: number ): number[] {\n  const parents = new Map<TreeNode, TreeNode>();\n  const seen
```\nfunction distanceK(  root: TreeNode | null,  target: TreeNode | null,  k: number ): number[] {\n  const parents = new Map<TreeNode, TreeNode>();\n  const seen = new Set();\n  const result: number[] = [];\n\n  function dfs(root: TreeNode, parent: TreeNode) {\n    if (root !== null) {\n      parents.set(root, parent); // store parents of each node to be able to traverse up and down within tree\n\n      dfs(root.left, root);\n      dfs(root.right, root);\n    }\n  }\n  dfs(root, null); \n  \n   const queue = [target];\n   \n  seen.add(target);\n  seen.add(null);\n  \n  let distance = 0;\n  while (queue.length > 0) {\n  \n    const range = queue.length;\n\t\n    for (let index = 0; index < range; index++) {\n      if (k == distance) {\n        while (queue.length > 0) {\n          result.push(queue.shift()?.val);\n        }\n        console.log(result);\n        return result;\n      }\n      const node = queue.shift();\n\n      if (node.left !== null && !seen.has(node.left)) {\n        seen.add(node.left);\n        queue.push(node.left);\n      }\n      if (node.right !== null && !seen.has(node.right)) {\n        seen.add(node.right);\n        queue.push(node.right);\n      }\n      const parent = parents.get(node);\n      if (parent !== null && !seen.has(parent)) {\n        seen.add(parent);\n        queue.push(parent);\n      }\n    }\n    distance++;\n  }\n\n  return [];\n}\n\n```
['Depth-First Search', 'Breadth-First Search', 'TypeScript', 'JavaScript']