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. bzoj1010

    题解: 斜率优化dp f[i]=f[j]+(i-j+sum[i]-sum[j]-L)^2 然后斜率优化 代码: #include<bits/stdc++.h> typedef long l ...

  2. ural1517

    题解: 后缀数组 求一下最长公共字串 代码: #include<cstdio> #include<cmath> #include<algorithm> #inclu ...

  3. [Linux]Linux下Apache服务器配置

    Linux下Apache服务器配置 相关包: httpd-2.2.3-29.e15.i386.rpm                 //主程序包 httpd-devel-2.2.3-29.e15.i ...

  4. C++输出数组名

    1.如果C++输出的数组是char类型的,那么输出的就是数组中的元素. 2.如果使用的是其他类型的数组作为输出的话,那么就是一个16进制的地址. 3.还是那句话,对数组的操作,很多时候都是指针的操作, ...

  5. delete和delete[] 区别

    // DeleteAndDelete[].cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> ...

  6. AOP 实现自定义注解

    1.自定义注解2.编写 AOP3.测试 1.自定义注解 package com.base.yun.spring.aop; import java.lang.annotation.Documented; ...

  7. SharePoint Framework 在Visual Studio Code中调试你的本地解决方案

    博客地址:http://blog.csdn.net/FoxDave Visual Studio Code不知道大家都有没有,界面清爽,编辑快速,是一个非常好的前端开发工具.本文介绍如何使用Goog ...

  8. ClickOnce 和管理员权限

    有些程序需要管理员权限需要运行,同时又想用ClickOnce进行发布,这时候就麻烦了,两者是互斥的. 解决方案是,去掉管理员权限的要求,可以进行发布. 程序启动的时候,加载程序员权限的请求. 代码如下 ...

  9. java基础学习之final关键字

    final可以修饰类.方法.变量,一旦使用了final则将不能改变被修饰的对象的引用; 被final修饰的类不可以被继承 被final修饰的方法不可以被覆盖 被final修饰的变量一般为常量,只允许对 ...

  10. Node.js 回调函数 1) 阻塞 ,同步 2) 非阻塞 ,异步.

    1.阻塞. 同步. 1) 读取的文件: input.txt 菜鸟教程官网地址:www.runoob.com 2) main.js var fs = require("fs"); / ...