Convert Tree to Graph 🍀 ➡️  📈
Convert the given tree into a graph and perform a BFS traversal using the target Node on the graph  that you\'ve created.\n\n# Java Code\n\nclass Pair\n{\n    i
> ## *Convert the given tree into a graph and perform a BFS traversal using the target Node on the graph  that you\'ve created.*\n\n# Java Code\n```\nclass Pair\n{\n    int value = 0;\n    int count = 0;\n    Pair(int value,int count)\n    {\n        this.value = value;\n        this.count = count;\n    }\n}\nclass Solution {\n    public void addEdge(int source,int destination,HashMap<Integer,ArrayList<Integer>> graph)\n    {\n        if(graph.containsKey(source) == false)\n        {\n            ArrayList<Integer> arr = new ArrayList<>();\n            arr.add(destination);\n            graph.put(source,arr);\n        }\n        else\n        {\n            ArrayList<Integer> arr = graph.get(source);\n            arr.add(destination);\n            graph.put(source,arr);   \n        }\n    }\n    public void buildGraph(TreeNode root,HashMap<Integer,ArrayList<Integer>> graph)\n    {\n        Queue<TreeNode> queue = new LinkedList<>();\n        queue.add(root);\n        while(queue.size() > 0)\n        {\n            TreeNode curr = queue.poll();\n            if(curr.left!=null)\n            {\n                addEdge(curr.val,curr.left.val,graph);\n                addEdge(curr.left.val,curr.val,graph);\n                queue.offer(curr.left);\n            }\n            if(curr.right!=null)\n            {\n                addEdge(curr.val,curr.right.val,graph);\n                addEdge(curr.right.val,curr.val,graph);\n                queue.offer(curr.right);\n            }\n        }\n    }\n    public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {\n        List<Integer> result = new ArrayList<>();\n        HashMap<Integer,ArrayList<Integer>> graph = new HashMap<>();\n        buildGraph(root,graph);\n        if(graph.containsKey(target.val) == false)  return result;\n        Queue<Pair> queue = new LinkedList<>();\n        queue.offer(new Pair(target.val,0));\n        HashSet<Integer> hset = new HashSet<>();\n        hset.add(target.val);\n        while(queue.size() > 0)\n        {\n            Pair p = queue.poll();\n            int v = p.value;\n            int c = p.count;\n            if(c == k)\n                result.add(v);\n            for(int item : graph.get(v))\n            {\n                if(hset.contains(item) == false)\n                {\n                    hset.add(item);\n                    queue.offer(new Pair(item,c+1));\n                }\n            }\n        }\n        return result;\n    }\n}\n```\n---\n### *Please don\'t forget to upvote if you\'ve liked my solution.* \u2B06\uFE0F\n---
['Depth-First Search', 'Breadth-First Search', 'Graph', 'Binary Tree', 'Java']