Print all nodes at distance k from a given node
<p><a href="https://i.stack.imgur.com/mHs2n.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mHs2n.jpg" alt="enter image description here"></a>
The tree does not have any parent pointer.
If this is the graph and target is 10 and k is 2, then the output should be 150, 160,8,14. <a href="http://www.geeksforgeeks.org/print-nodes-distance-k-given-node-binary-tree/" rel="nofollow noreferrer">http://www.geeksforgeeks.org/print-nodes-distance-k-given-node-binary-tree/</a> has a soluton which works though I am not able to understand it.</p>

<p>So I constructed a undirected graph from the tree as per the 1st answer. 
I think the complexity of my <code>printN</code> is not O(n). Can anyone please check that.
Thanks!</p>

<pre><code>public class Nth {
    private int targetV;

    private class Node {
        private int data;
        private Node left, right;

        public Node() {
        }

        public Node(int data) {
            this.data = data;
        }
    }

    private class GraphNode {
        private HashMap&lt;Integer, ArrayList&lt;Integer&gt;&gt; edgeMap;

        GraphNode() {
            edgeMap = new HashMap&lt;&gt;();
        }

        private void addEdge(int v, int w) {
            if (edgeMap.containsKey(v)) {
                if (!edgeMap.get(v).contains(w))
                    edgeMap.get(v).add(w);
            } else {
                edgeMap.put(v, new ArrayList&lt;&gt;());
                edgeMap.get(v).add(w);
            }
        }
    }

    public static void main(String[] args) {
        new Nth().job1();
    }

    private void job1() {
        Node root = new Node(20);
        root.left = new Node(8);
        root.left.left = new Node(4);
        root.left.right = new Node(12);
        root.left.right.left = new Node(10);
        root.left.right.left.left = new Node(50);
        root.left.right.left.left.left = new Node(150);
        root.left.right.left.left.left.left = new Node(1250);
        root.left.right.left.left.left.right = new Node(12512);
        root.left.right.left.left.right = new Node(160);
        root.left.right.left.right = new Node(60);
        root.left.right.right = new Node(14);
        root.right = new Node(22);
        GraphNode graphNode = new GraphNode();
        generateGraph(null, null, root, graphNode, 10);
        check(graphNode);
        // printN(graphNode, 2);
    }

private void printN(GraphNode graphNode, int k) {
    Set&lt;Integer&gt; items = new HashSet&lt;&gt;();
    Queue&lt;Integer&gt; queue = new ArrayDeque&lt;&gt;();
    queue.add(targetV);
    HashMap&lt;Integer, Boolean&gt; hashMap = new HashMap&lt;&gt;();
    hashMap.put(targetV, true);
    for (int e = 0; e &lt; k; e++) {
        items.clear();
        Queue&lt;Integer&gt; back = new ArrayDeque&lt;&gt;();
        back.addAll(queue);
        int xq = back.size();
        queue.clear();
        for (int i = 0; i &lt; xq; i++) {
            int polled = back.poll();
            ArrayList&lt;Integer&gt; list = graphNode.edgeMap.get(polled);
            list.forEach((x) -&gt; {
                if (!hashMap.containsKey(x)) {
                    queue.add(x);
                    hashMap.put(x, true);
                }
            });
        }
        items.addAll(queue);
    }

    System.out.println(Arrays.toString(items.toArray()));
}

    private void check(GraphNode graphNode) {
        for (Map.Entry&lt;Integer, ArrayList&lt;Integer&gt;&gt; pop : graphNode.edgeMap.entrySet()) {
            System.out.println(pop.getKey() + "\t\t\t" + Arrays.toString(pop.getValue().toArray()));
        }
    }

    private void generateGraph(Node parent, Node root, Node child, GraphNode graphNode, int target) {
        if (child == null &amp;&amp; root != null &amp;&amp; parent != null)
            graphNode.addEdge(root.data, parent.data);
        if (child == null) return;
        if (root != null) graphNode.addEdge(root.data, child.data);
        if (parent != null &amp;&amp; root != null) graphNode.addEdge(root.data, parent.data);
        if (child.data == target) targetV = child.data;
        generateGraph(root, child, child.left, graphNode, target);
        generateGraph(root, child, child.right, graphNode, target);
    }

}
</code></pre>

<p>My check fun prints:</p>

<pre><code>        160         [50]
        1250        [150]
        1251        [150]
        4           [8]
        8           [4, 20, 12]
        10          [50, 12, 60]
        12          [10, 8, 14]
        14          [12]
        50          [150, 10, 160]
        20          [8, 22]
        150         [1250, 50, 1251]
        22          [20]
        60          [10]
</code></pre>