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.

题意是让我们在一颗二叉树中,给定节点 Target, 寻找和target节点距离 为 K的所有节点,我们可以把这颗树看成一个无向图, 没有顺序就意味着,二叉树的旁边的指节也是可以算距离的。 
我们可以先把二叉树转化为无向图,再在图中进行 BFS 搜索应该就可以得到答案。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) { List<Integer> resList = new ArrayList<Integer> ();
resList.add(target.val); Set<Integer> visited = new HashSet<>();
visited.add(target.val); Map<Integer, List<Integer>> graph = new HashMap<>();
treeToGraph(graph, null, root); if(graph.isEmpty() && target == root && K == 0){
return resList;
}
else if(graph.isEmpty()){
return new ArrayList<Integer> ();
} for(int i=0; i<K; i++){
List<Integer> temp = new ArrayList<>();
for(Integer v : resList){
for(Integer e : graph.get(v)){
if(!visited.contains(e)){
visited.add(e);
temp.add(e);
}
}
}
resList = temp;
}
return resList; } public void treeToGraph(Map<Integer, List<Integer>> map, TreeNode parent, TreeNode child){
if(parent != null && child != null){
if(map.containsKey(parent.val)){
map.get(parent.val).add(child.val);
}
else{
List<Integer> list = new ArrayList<>();
list.add(child.val);
map.put(parent.val, list);
}
if(map.containsKey(child.val)){
map.get(child.val).add(parent.val);
}
else{
List<Integer> list = new ArrayList<>();
list.add(parent.val);
map.put(child.val, list);
}
}
if(child.left != null){
treeToGraph(map, child, child.left);
}
if(child.right != null){
treeToGraph(map, child, child.right);
} }
}

  

  

LeetCode – All Nodes Distance K in Binary Tree的更多相关文章

  1. [LeetCode] All Nodes Distance K in Binary Tree 二叉树距离为K的所有结点

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  2. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

  3. leetcode 863. All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  4. [Swift]LeetCode863. 二叉树中所有距离为 K 的结点 | All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a targetnode, and an integer value K. Return a lis ...

  5. 863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点

    [抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Retur ...

  6. [LC] 863. All Nodes Distance K in Binary Tree

    We are given a binary tree (with root node root), a target node, and an integer value K. Return a li ...

  7. 863. All Nodes Distance K in Binary Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  8. 距离为K的节点 All Nodes Distance K in Binary Tree

    2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...

  9. [Leetcode] 863. All Nodes Distance K in Binary Tree_ Medium tag: BFS, Amazon

    We are given a binary tree (with root node root), a target node, and an integer value `K`. Return a ...

随机推荐

  1. Linux学习: 触摸屏驱动

    一.Linux输入子系统的结构: 二.触摸屏驱动代码: s3c_ts.c #include <linux/errno.h> #include <linux/kernel.h> ...

  2. idea 一些插件配置

    接触maven快2年了吧,对maven还是一知半解其实.得到了一些教训,就是少转牛角尖,多把握实际需要的东西,一口一口吃饭. 插件化很常见了.这里记录idea使用的jetty插件 和tomcat插件和 ...

  3. L319 Zigbee test coding- field test fail-base on EFR32MG1

    1 Test coding Zigbee  test of Tx power and frequency for every channel. Testing Procedure1) Power up ...

  4. 【python】pandas display选项

    import pandas as pd 1.pd.set_option('expand_frame_repr', False) True就是可以换行显示.设置成False的时候不允许换行 2.pd.s ...

  5. Vue数组操作不刷新视图问题的解决

    最近使用Vue2.0开发项目,有一个列表使用v-for绑定到数组,按照Vue的推荐方案,使用push.splice.this.$set三个变异方法操作数组.发现在添加页面,三个方法都能及时刷新视图:但 ...

  6. 什么是wsgi,uwsgi,uWSGI

    WSGI: web服务器网关接口,是一套协议.用于接收用户请求将请求进行初次封装,然后将请求交给web框架 实现wsgi协议的模块: 1,wsgiref,本质就是编写一个socket服务端,用于接收用 ...

  7. java学习笔记27(File类)

    File类: 定义:文件和目录径的抽象表示形式, Java中将路径或者文件封装成File对象 1.File类的静态成员变量 package com.zs.Demo2; import java.io.F ...

  8. LeetCode--122、167、169、189、217 Array(Easy)

    122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...

  9. angularJS使用编写KindEditor,UEidtor,jQuery指令,双重绑定

    第一步 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8 ...

  10. spring boot热启动

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...