Java three methods
\n// BFS + HashSet\nclass Solution {\n    Map<TreeNode, TreeNode> parentMap;\n    public void buildParentMap(TreeNode root) {\n        if (root == null) {\n    
```\n// BFS + HashSet\nclass Solution {\n    Map<TreeNode, TreeNode> parentMap;\n    public void buildParentMap(TreeNode root) {\n        if (root == null) {\n            return ;\n        }\n        if(root.left != null) {\n            parentMap.put(root.left, root);\n        }\n        if(root.right != null) {\n            parentMap.put(root.right, root);\n        }\n        buildParentMap(root.left);\n        buildParentMap(root.right);\n    }\n    public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {\n        if (k == 0) {\n            return Arrays.asList(target.val);\n        }\n        parentMap = new HashMap<>();\n        List<Integer> ans = new ArrayList<>();\n        buildParentMap(root);\n        Set<TreeNode> visited = new HashSet<>();\n        Queue<TreeNode> queue = new LinkedList<>();\n        queue.add(target);\n        visited.add(target);\n        while (!queue.isEmpty() && k > 0) {\n            k--;\n            int size = queue.size();\n            for (int i = 0; i < size; i++) {\n                TreeNode node = queue.remove();\n                \n                if (parentMap.containsKey(node) && !visited.contains(parentMap.get(node))) {\n                    queue.add(parentMap.get(node));\n                    visited.add(parentMap.get(node));\n                }\n                if (!visited.contains(node.left) && node.left != null) {\n                    queue.add(node.left);\n                    visited.add(node.left);\n                }\n                if (!visited.contains(node.right) && node.right != null) {\n                    queue.add(node.right);\n                    visited.add(node.right);\n                }\n            }\n            if (k == 0) {\n                while (!queue.isEmpty()) {\n                    ans.add(queue.remove().val);\n                }\n            }\n\n        }\n        return ans;\n    }\n}\n```\n\n```\n// DFS + HashSet\nclass Solution {\n    Map<TreeNode, TreeNode> parentMap;\n    List<Integer> ans;\n    public void buildParentMap(TreeNode root) {\n        if (root == null) {\n            return ;\n        }\n        if(root.left != null) {\n            parentMap.put(root.left, root);\n        }\n        if(root.right != null) {\n            parentMap.put(root.right, root);\n        }\n        buildParentMap(root.left);\n        buildParentMap(root.right);\n    }\n    public void dfs(TreeNode root, Set<TreeNode> visited, int k) {\n        if (root == null) {\n            return ;\n        }\n        visited.add(root);\n        if (k == 0) {\n            ans.add(root.val);\n            return ;\n        }\n        if (parentMap.containsKey(root) && !visited.contains(parentMap.get(root))) {\n            dfs(parentMap.get(root), visited, k - 1);\n        }\n        if (!visited.contains(root.left)) {\n            dfs(root.left, visited, k - 1);\n        }\n        if (!visited.contains(root.right)) {\n            dfs(root.right, visited, k - 1);\n        }\n    }\n    public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {\n        parentMap = new HashMap<>();\n        ans = new ArrayList<>();\n        buildParentMap(root);\n        Set<TreeNode> visited = new HashSet<>();\n        dfs(target, visited, k);\n        return ans;\n    }\n}\n```\n\n```\n// DFS + Previous Node\nclass Solution {\n    Map<TreeNode, TreeNode> parentMap;\n    List<Integer> ans;\n    public void buildParentMap(TreeNode root) {\n        if (root == null) {\n            return ;\n        }\n        if(root.left != null) {\n            parentMap.put(root.left, root);\n        }\n        if(root.right != null) {\n            parentMap.put(root.right, root);\n        }\n        buildParentMap(root.left);\n        buildParentMap(root.right);\n    }\n    public void dfs(TreeNode root, TreeNode prev, int k) {\n        if (root == null) {\n            return ;\n        }\n        if (k == 0) {\n            ans.add(root.val);\n            return ;\n        }\n        if (parentMap.containsKey(root) && parentMap.get(root) != prev) {\n            dfs(parentMap.get(root), root, k - 1);\n        }\n        if (root.left != prev) {\n            dfs(root.left, root, k - 1);\n        }\n        if (root.right != prev) {\n            dfs(root.right, root, k - 1);\n        }\n    }\n    public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {\n        parentMap = new HashMap<>();\n        ans = new ArrayList<>();\n        buildParentMap(root);\n        dfs(target, null, k);\n        return ans;\n    }\n}\n```
['Depth-First Search', 'Breadth-First Search', 'Java']