[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 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
targetnode is a node in the tree. 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的更多相关文章
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 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 ...
- 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 ...
- 863. All Nodes Distance K in Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- [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 ...
- [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 ...
- [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 ...
- 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 ...
- 距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...
随机推荐
- Java 布尔运算
章节 Java 基础 Java 简介 Java 环境搭建 Java 基本语法 Java 注释 Java 变量 Java 数据类型 Java 字符串 Java 类型转换 Java 运算符 Java 字符 ...
- thinkphp配置到二级目录,不配置到根目录,访问除首页的其他路径都是404报错
1.在nginx的配置里面,进行重定向 vi /etc/nginx/conf.d/default.conf 2.进入编辑 location /thinkphp/public { if (!-e $re ...
- CF97B Superset超级集合
CF97B Superset 这题主要是构造难想.看看数据范围发现连\(O(n^2)\)都被卡了,然后 考试的名称提醒我 想到了分治. 坐标按横坐标为关键字排序后找中间的点进行分治不是点分治qwq. ...
- 超低功耗2.4G收发一体: SI24R1
Si24R1是一颗工作在2.4GHz ISM频段,专为低功耗无线场合设计,集成嵌入式ARQ基带协议引擎的无线收发器芯片.工作频率范围为2400MHz-2525MHz,共有126个1MHz带宽的信道.同 ...
- 023-PHP常用数组函数
<?php $colors = array("red", "green", "blue","yellow"); p ...
- spring源码 继承AttributeAccessor的BeanDefinition接口
/** * A BeanDefinition describes a bean instance, which has property values, * constructor argument ...
- 手动搭建简单的vue项目
创建项目根目录 切换到根目录下 , 并执行 npm init , 所有选项都默认即可. 安装 webpack webpack-cli vue vue-loader 添加项目结构
- .NET via C#笔记17——委托
一.委托的内部实现 C#中的委托是一种类型安全的回调函数,假设有这样一个委托: internal delegate void Feedback(int value); 编译器会生成一个类: inter ...
- css渐变实现
body{ width: 100%; height: 100%; overflow: hidden; } *{ margin: 0px; padding: 0px; font-size: 0px; } ...
- JavaSE--日志
参考 https://www.cnblogs.com/hanszhao/p/9754419.html https://www.cnblogs.com/chenhongliang/p/5312517.h ...