C# / JavaScript, Graph + BFS, O(N) Time and Space
C#\n\npublic class Solution {\n    public IList<int> DistanceK(TreeNode root, TreeNode target, int K) {\n        var queue = new Queue<int>();\n        var stac
C#\n```\npublic class Solution {\n    public IList<int> DistanceK(TreeNode root, TreeNode target, int K) {\n        var queue = new Queue<int>();\n        var stack = new Stack<TreeNode>();\n        var graph = new Dictionary<int, IList<int>>();\n        var visited = new HashSet<int>();\n        \n        stack.Push(root);\n        queue.Enqueue(target.val);\n        \n        while (stack.Count > 0) {\n            var node = stack.Pop();\n\n            if (!graph.ContainsKey(node.val)) { graph[node.val] = new List<int>(); }\n            if (node.left != null) {\n                if (!graph.ContainsKey(node.left.val)) { graph[node.left.val] = new List<int>(); }\n                \n                graph[node.left.val].Add(node.val);\n                graph[node.val].Add(node.left.val);\n                \n                stack.Push(node.left);\n            }\n            if (node.right != null) {\n                if (!graph.ContainsKey(node.right.val)) { graph[node.right.val] = new List<int>(); }\n                \n                graph[node.right.val].Add(node.val);\n                graph[node.val].Add(node.right.val);\n                \n                stack.Push(node.right);\n            }\n        }\n        \n        while (K-- > 0) {\n            int size = queue.Count;\n            for (int i = 0; i < size; i++) {\n                int node = queue.Dequeue();\n                \n                visited.Add(node);\n                var neighbors = graph[node];\n                \n                foreach (var neighbor in neighbors) {\n                    if (visited.Contains(neighbor)) { continue; }\n                    queue.Enqueue(neighbor);\n                }\n            }\n        }\n        \n        return queue.ToList();\n    }\n}\n```\n\nJavaScript\n```\nconst distanceK = (root, target, K) => {\n    if (!root) { return []; }\n    \n    let graph = {}, stack = [root], queue = [target.val], visited = {};\n    \n    while (stack.length) {\n        let node = stack.pop();\n        \n        if (!graph[node.val]) { graph[node.val] = []; }\n        \n        if (node.left) {\n            if (!graph[node.left.lva]) { graph[node.left.val] = []; }\n            graph[node.left.val].push(node.val);\n            graph[node.val].push(node.left.val);\n            \n            stack.push(node.left);\n        }\n        \n        if (node.right) {\n            if (!graph[node.right.val]) { graph[node.right.val] = []; }\n            graph[node.right.val].push(node.val);\n            graph[node.val].push(node.right.val);\n            \n            stack.push(node.right);\n        }\n    }\n    \n    while (K--) {\n        let size = queue.length;\n        \n        for (let i = 0; i < size; i++) {\n            let node = queue.shift();\n            let neighbors = graph[node];\n            \n            visited[node] = true;\n\n            for (let neighbor of neighbors) {\n                if (visited[neighbor]) { continue; }\n                queue.push(neighbor);\n            }\n        }\n    }\n    \n    return queue;\n};\n```
[]