[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进行层次遍历即可. 这种解法 ...
随机推荐
- Ado.NET SQLHelper(2)
测试发现前面发的那个功能太简单,不能调用getdate()等内部函数. 完善后重载了insert和update两个功能,将函数作为字符串传入SQL语句构造,需要的可以试用一下 using Sys ...
- BInder机制总结
BInder机制 Linux内核的基础知识 进程隔离/虚拟地址空间 操作系统当中为了保证进程间互不干扰,设计了进程隔离的技术,避免了一个进程去操作另一个进程的数据.进程隔离用到了虚拟地址空间,不同进程 ...
- 埃及分数问题 迭代加深搜索/IDA*
输入整数a,b (0<a<b<500) ,输出最佳表达式 使得加数个数尽量小,如果加数个数相同,则最小的分数越大越好 ,输出表达式 考虑从小到大枚举深度上限maxd,每次执行只考虑深 ...
- 配置thinkphp对mysql线上线下切换不同环境的配置 - (mysql-thinkphp) (1)
1.先打印出配置项的信息,在Index控制器下面的index->index里面设置 namespace app\index\controller; class Index { public fu ...
- JVM--a == (a = b)基于栈的解释器执行过程
前言 在翻阅ConcurrentLinkedQueue的代码的时候,发现这样一段代码在JDK源码中总是出现. t != (t = tail) 原先总是以为这不就是 t != t ?很是纳闷,遂Demo ...
- 实验吧-密码学-他的情书(进一步了解js代码调试和console.log)
打开网站,在白色背景下的任一点上点击鼠标,白色部分都会消失(包括password输入框),那么就无法输入. 查看源码,发现是明显的从源码解决问题. 火狐F12查看器查看源码(如果是简单的操作,可以vi ...
- mysql视图使用方法
1.为什么要使用视图 对于复杂的查询,往往是有多个数据表进行关联查询而得到,而这种语句往往比较复杂,也可能非常频繁的使用.比如: select 字段一,字段二.字段三, from 数据表1 join ...
- StarUML 3.0.2安装激活
文章参考:https://blog.csdn.net/sam_shan/article/details/80585240 1.下载StarUML:http://staruml.io/,一直下一步安装. ...
- Arduino --structure
The elements of Arduino (C++) code. Sketch loop() setup() Control Structure break continue do...whil ...
- PHP的变量作用域-常亮-全局变量-表单提交变量
一.变量的作用域 作用域是指在一个脚本中某个变量在哪些地方可以使用或可见. 内置超级全局变量可以在脚本的任何地方使用和可见. 常量,一旦被声明,将可以在全局可见.也就是说,他们可以在函数内外使用. 在 ...