# All Nodes Distance K in Binary Tree

``````/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
class Node{
Node(int x) {this.val = x;}
int val;
Node p;
Node l;
Node r;
}
public List<Integer> distanceK(TreeNode root, TreeNode target, int k) {
Node nr = new Node(root.val);
dfs(root, null, nr, null);
List<Integer> res = new ArrayList<>();
Node nt = find(target.val, nr);
Set<Node> set = new HashSet<>();
while(k > 0){
int size = q.size();
for(int i = 0; i < size; i++){
Node c = q.poll();
if(c.l != null && !set.contains(c.l)){
}
if(c.r != null && !set.contains(c.r)){
}
if(c.p != null && !set.contains(c.p)){
}
}
k--;
}
for(Node cc : q)
return res;
}
public Node find(int val, Node nr) {
if(nr == null)
return null;
if(nr.val == val)
return nr;
Node l = find(val, nr.l);
Node r = find(val, nr.r);
return l == null ? r : l;
}
public void dfs(TreeNode cur, TreeNode parent, Node nr, Node nrp){
if(cur == null)
return;
nr.val = cur.val;
nr.p = nrp;
if(cur.left != null){
nr.l = new Node(cur.left.val);
dfs(cur.left, cur, nr.l, nr);
}
if(cur.right != null){
nr.r = new Node(cur.right.val);
dfs(cur.right, cur, nr.r, nr);
}
}
}``````