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:

  1. The given tree is non-empty.
  2. Each node in the tree has unique values 0 <= node.val <= 500.
  3. The target node is a node in the tree.
  4. 0 <= K <= 1000.
/**
* 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<>();
Queue<TreeNode> queue = new LinkedList<>();
Set<TreeNode> visited = new HashSet<>();
buildMap(root, null);
queue.offer(target);
visited.add(target);
while(!queue.isEmpty()) {
if (K == 0) {
int resSize = queue.size();
for (int i = 0; i < resSize; i++) {
res.add(queue.poll().val);
}
return res;
}
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode cur = queue.poll();
for (TreeNode nei : map.get(cur)) {
if (visited.contains(nei)) {
continue;
}
queue.offer(nei);
visited.add(nei);
}
}
K--;
}
return res;
} private void buildMap(TreeNode node, TreeNode parent) {
if (node == null) {
return;
}
map.put(node, new ArrayList<>());
if (parent != null) {
map.get(parent).add(node);
map.get(node).add(parent);
}
buildMap(node.left, node);
buildMap(node.right, node);
}
}

[LC] 863. All Nodes Distance K in Binary Tree的更多相关文章

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

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

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

  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. 863. All Nodes Distance K in Binary Tree

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

  5. [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 ...

  6. [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 ...

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

  8. LeetCode – 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 ...

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

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

随机推荐

  1. .net core文件系统简介

    在asp.net core程序中,我们可以通过如下代码开启对Web 根目录内的文件静态访问功能: app.UseStaticFiles(); 如果要提供更高级的选项,例如:将其它的物理文件夹下的文件作 ...

  2. ERP开发准备

    Delphi 开发 ERP [1] 准备 使用的控件FireDac.DevExpress.FastReport. FireDac:新一代的数据库控件. DevExpress:界面UI. FastRep ...

  3. 08 SSM整合案例(企业权限管理系统):09.用户和角色操作

    04.AdminLTE的基本介绍 05.SSM整合案例的基本介绍 06.产品操作 07.订单操作 08.权限控制 09.用户和角色操作 10.权限关联 11.AOP日志 09.用户和角色操作 1. 用 ...

  4. Django 项目搭建

    django(mvt结构) 虚拟环境 创建虚拟环境 mkvirtualenv django_py3 -p python3 切换虚拟环境 wokeon 虚拟环境名称 删除虚拟环境 rmvirtualen ...

  5. P1044 火星数字

    P1044 火星数字 转跳点:

  6. Oracle的操作经验

    采用Oracle进行sql语句 建表并设置主键,主键自增,某一字段唯一性约束等 <---如果表存在则删除---> declare num number; begin select coun ...

  7. redis以服务模式开机启动

    第一步 修改redis为后台启动 vim /usr/redis/redis.conf #路径根据实际情况决定 # By default Redis does not run as a daemon. ...

  8. 【Cantor表】蒟蒻题解

    原题:传送门 (上图摘自网站OpenJudge - NOI题库2.1 Cantor表) 本蒟蒻的题解,让大神们见笑了! 首先,进行找规律. 大家可以发现: 1.当分子是一的时候,且分子和分母的和是偶数 ...

  9. Babel(1)认识Babel

    阅读文档 Babel中文网 关于 Babel 你必须知道的 如何写好.babelrc?Babel的presets和plugins配置解析 不容错过的 Babel 7 知识汇总 一口(很长的)气了解 b ...

  10. servlet 之 GenericServlet抽象类详解

    GenericSerlvet抽象类源码如下: package javax.servlet; import java.io.IOException; import java.util.Enumerati ...