Description
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.
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2
Output: [7,4,1]
Explanation:
The nodes that are a distance 2 from the target node (with value 5)
have values 7, 4, and 1.
Note that the inputs “root” and “target” are actually TreeNodes.
The descriptions of the inputs above are just serializations of these objects.
Note:
The given tree is non-empty.
Each node in the tree has unique values 0 <= node.val <= 500.
The target node is a node in the tree.
0 <= K <= 1000.
Solution
给一棵二叉树,以及一个target node和K距离,找到书中所有离target node K 距离的nodes。
For this question, I convert it to a graph to perform BFS.
Using a Map<TreeNode, List<TreeNode>> to construct a undirected graph with each nodes as nodes, child-parent and parent-child relation as edges.
First, build graph, with node and his parent. if node == null return , if map does not contain this node, add it and list. If parent != null, add two links from node to parent and parent to node, then build graph recursively.
Next, perform BFS. Using a set to document visited node, and a queue. For each level, if K == 0, add remain element in queue to res and return. Or we poll out every node of this level then add their unvisited child. After each level’s traverse, K–.
Code
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
Map<TreeNode, List<TreeNode>> map = new HashMap<>();
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
List<Integer> res = new ArrayList<>();
if (root == null || K < 0){
return res;
}
//build a graph by connect child-parent and parent-child edges
buildGraph(root, null);
Set<TreeNode> visited = new HashSet<>();
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(target);
visited.add(target);
while (!queue.isEmpty()){
int size = queue.size();
if (K == 0){
for (int i = 0; i < size; i++){
res.add(queue.poll().val);
}
return res;
}
for (int i = 0; i < size; i++){
TreeNode temp = queue.poll();
for (TreeNode node : map.get(temp)){
if (visited.contains(node)){
continue;
}
queue.offer(node);
visited.add(node);
}
}
K--;
}
return res;
}
private void buildGraph(TreeNode node, TreeNode parent){
if (node == null){
return;
}
//If does not contain such node, add a new list of neighbor.
if (!map.containsKey(node)){
map.put(node, new ArrayList<TreeNode>());
if (parent != null){
//connect child with parent and parent with child
//because it is a pointer, we do not need to put it back again.
map.get(node).add(parent);
map.get(parent).add(node);
}
//build graph recursively
buildGraph(node.left, node);
buildGraph(node.right, node);
}
}
}
Time Complexity: O(n)
Space Complexity: O(n)
Review
广度优先和深度优先只是遍历节点的顺序不同,对于同一个图,如果是用二维数组表示的图,时间复杂度均为O(n^2);如果使用邻接表,e为edge数,则时间复杂度为O(n + e);