✅Most easy and intuitive solution | Beats 97.37% ✨
Complexity
Time complexity: O(n)

Space complexity: O(n)
UPVOTE if you found it helpfull :)

# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# ___UPVOTE if you found it helpfull :)___
```java []
class Solution {
    HashMap<TreeNode, TreeNode> prev = new HashMap<>();
    public void previous(TreeNode root, TreeNode previous) {
        if (root == null) return;
        prev.put(root, previous);
        
        previous(root.left, root);
        previous(root.right, root);

        return;
    }
    public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
        //Step-I : Store previous
        previous(root, null);

        //Step-II : bfs from target
        Queue<TreeNode> q = new LinkedList<>();
        HashSet<TreeNode> vis = new HashSet<>();
        vis.add(target);
        vis.add(null);
        q.add(target);
        int count = 0;

        while (!q.isEmpty()) {
            int size = q.size();

            if (count == k) break;
            while(size-- > 0) {
                TreeNode curr = q.poll();
                if (!vis.contains(curr.left)) {
                    q.offer(curr.left);
                    vis.add(curr.left);
                }
                
                if (!vis.contains(curr.right)) {
                    q.offer(curr.right);
                    vis.add(curr.right);
                }
                if (!vis.contains(prev.get(curr))) {
                    q.offer(prev.get(curr));
                    vis.add(prev.get(curr));
                }
            }
            count++;
        }

        List<Integer> res = new ArrayList<>();
        while(!q.isEmpty()) {
            res.add(q.poll().val);
        }
        return res;
    }
}
```
['Hash Table', 'Math', 'Backtracking', 'Tree', 'Depth-First Search', 'Breadth-First Search', 'Queue', 'Ordered Map', 'Binary Tree', 'Java']