[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
target
node 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进行层次遍历即可. 这种解法 ...
随机推荐
- mysql limit查询入门
- NO10 查看Linux操作系统-版本-内核-Linux分区
·看Linux系统: [root@localhost ~]# uname -m (看操作系统)x86_64[root@localhost ~]# uname -a (看操作系统)Linux lo ...
- LeetCode1005 K次取反后最大化的数组和(贪心+Java简单排序)
题目: 给定一个整数数组 A,我们只能用以下方法修改该数组:我们选择某个个索引 i 并将 A[i] 替换为 -A[i],然后总共重复这个过程 K 次.(我们可以多次选择同一个索引 i.) 以这种方式修 ...
- Android之UI适配
LinearLayout为线性布局按照垂直或者水平来进行排列,默认是按照水平来进行排列的,其中orientation属性是指定当前布局排列的方向 wrap_content为包裹内容 match_p ...
- P1047 编程团体赛
转跳点:
- Mysql 模糊查询 转义字符
MySQL的转义字符“\” \0 一个ASCII 0 (NUL)字符. \n 一个新行符. \t 一个定位符. \r 一个回车符. \b 一个退 ...
- HDU - 1698 Just a Hook (线段树---区间修改)
题意:n个棍子,初始值全为1,给定Q个区间,分别赋值,问n个棍子的总值. 分析:lazy标记主要体现在update上. 当l <= L && R <= r时,该结点的子结点 ...
- JS - n次方计算
pow 方法返回底表达式的指定次幂. Math.pow(base, exponent) 参数base 必选项.表达式底的值. exponent 必选项.表达式的指数值.
- 第二阶段scrum-6
1.整个团队的任务量: 2.任务看板: 会议照片: 产品状态: 消息收发功能正在制作
- 干货分享:常见的留学生Essay写作逻辑结构
任何一种类型的Essay写作都应遵循一种逻辑结构,Long Essay可能在不同部分依据情况使用不同的逻辑结构.本文将为大家分享六种常见留学生Essay写作逻辑结构,为方便阅读本文采用中英文对照方式. ...