题目描述
We are given a binary tree (with root node root), a target node, and an integer value K.
Return a list of the values of all nodes that have a distance K from the target node. The answer can be returned in any order.
题目链接
https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
方法思路
Approach1:Annotate Parent
Intuition
If we know the parent of every node x, we know all nodes that are distance 1 from x. We can then perform a breadth first search from the target node to find the answer.
Algorithm
We first do a depth first search where we annotate every node with information about it’s parent.
After, we do a breadth first search to find all nodes a distance K from the target.
class Solution {
//Runtime: 3 ms, faster than 61.70%
//Memory Usage: 37.7 MB, less than 9.14%
Map<TreeNode, TreeNode> parent;
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
parent = new HashMap();
dfs(root, null);
Queue<TreeNode> queue = new LinkedList();
queue.add(null);
queue.add(target);
Set<TreeNode> seen = new HashSet();
seen.add(target);
seen.add(null);
int dist = 0;
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
if (node == null) {
if (dist == K) {
List<Integer> ans = new ArrayList();
for (TreeNode n: queue)
ans.add(n.val);
return ans;
}
queue.offer(null);
dist++;
} else {
if (!seen.contains(node.left)) {
seen.add(node.left);
queue.offer(node.left);
}
if (!seen.contains(node.right)) {
seen.add(node.right);
queue.offer(node.right);
}
TreeNode par = parent.get(node);
if (!seen.contains(par)) {
seen.add(par);
queue.offer(par);
}
}
}
return new ArrayList<Integer>();
}
public void dfs(TreeNode node, TreeNode par) {
if (node != null) {
parent.put(node, par);
dfs(node.left, node);
dfs(node.right, node);
}
}
}
Approach2: