题目
题目链接:
https://www.nowcoder.com/practice/e280b9b5aabd42c9b36831e522485622
思路
图,队列
构件图,直接从target出发,扩展到第k层就是答案
Java代码
import java.util.*;/** public class TreeNode {* int val = 0;* TreeNode left = null;* TreeNode right = null;* public TreeNode(int val) {* this.val = val;* }* }*/public class Solution {/*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param root TreeNode类* @param target int整型* @param k int整型* @return int整型一维数组*/public ArrayList distanceKnodes (TreeNode root, int target, int k) {//构建图TreeNode cur = root;Map<Integer, List<Integer>> graph = new HashMap<>();//BFS,二叉树的层序遍历Queue<TreeNode> q = new LinkedList<>();q.add(cur);graph.put(cur.val, new ArrayList<>());while (!q.isEmpty()) {TreeNode pop = q.poll();if (pop.left != null) {q.add(pop.left);graph.get(pop.val).add(pop.left.val);graph.put(pop.left.val, new ArrayList<>());graph.get(pop.left.val).add(pop.val);}if (pop.right != null) {q.add(pop.right);graph.get(pop.val).add(pop.right.val);graph.put(pop.right.val, new ArrayList<>());graph.get(pop.right.val).add(pop.val);}}//图的BFSQueue<Integer> queue = new LinkedList<>();queue.add(target);Set<Integer> visited = new HashSet<>();visited.add(target);while (k >= 0) {k--;int size = queue.size();for (int i = 0; i < size ; i++) {int pop = queue.poll();for (Integer next : graph.get(pop)) {if (visited.contains(next)) continue;queue.add(next);visited.add(next);}}if ( k == 0) break;}ArrayList<Integer> ans = new ArrayList <>();int size = queue.size();for (int i = 0; i < size ; i++) {ans.add( queue.poll());}return ans;}
}
Go代码
package mainimport . "nc_tools"/** type TreeNode struct {* Val int* Left *TreeNode* Right *TreeNode* }*//*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可*** @param root TreeNode类* @param target int整型* @param k int整型* @return int整型一维数组*/
func distanceKnodes(root *TreeNode, target int, k int) []int {//BFSgraph := map[int][]int{} //构件图//层序遍历二叉树q1 := []*TreeNode{}q1 = append(q1, root)graph[root.Val] = []int{}for len(q1) > 0 {size := len(q1)q1bak := []*TreeNode{}for i := 0; i < size; i++ {pop := q1[i]a := pop.Valif pop.Left != nil {q1bak = append(q1bak, pop.Left)b := pop.Left.Valgraph[a] = append(graph[a], b)graph[b] = []int{a}}if pop.Right != nil {q1bak = append(q1bak, pop.Right)c := pop.Right.Valgraph[a] = append(graph[a], c)graph[c] = []int{a}}}q1 = q1bak}//图的BFSq := []int{target}visited := map[int]bool{}visited[target] = truefor k >= 0 {k--size := len(q)qbak := []int{}for i := 0; i < size; i++ {pop := q[i]nexts := graph[pop]for _, next := range nexts {_, ok := visited[next]if ok {continue}qbak = append(qbak, next)visited[next] = true}}q = qbakif k == 0 {break}}return q
}
PHP代码
<?php/*class TreeNode{var $val;var $left = NULL;var $right = NULL;function __construct($val){$this->val = $val;}
}*//*** 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可** * @param root TreeNode类 * @param target int整型 * @param k int整型 * @return int整型一维数组*/
function distanceKnodes( $root , $target , $k )
{//BFS$graph = array(); //构建图//二叉树的层序遍历$q1 = [$root];$graph[$root->val] = array();while (count($q1) >0){$q1bak=array();$size = count($q1);for($i=0;$i<$size;$i++){$pop = $q1[$i];$a = $pop->val;if($pop->left!=null){$q1bak[count($q1bak)] = $pop->left;$b = $pop->left->val;$graph[$a][count($graph[$a])] = $b;$graph[$b]=[$a];}if($pop->right!=null){$q1bak[count($q1bak)] = $pop->right;$c = $pop->right->val;$graph[$a][count($graph[$a])] = $c;$graph[$c]=[$a];}}$q1=$q1bak;}//图的BFS$q = [$target];$visited = [$target=>$target];while ($k>=0){$k--;$size = count($q);$qbak = array();for($i=0;$i<$size;$i++){$nexts = $graph[$q[$i]];foreach ($nexts as $next){if(isset($visited[$next])) continue;$qbak[count($qbak)] =$next;$visited[$next] = $next;}}$q = $qbak;if($k ==0) break;}return $q;
}