距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37
问题描述:
给定一个二叉树(具有根结点 root), 一个目标结点 target ,和一个整数值 K 。
返回到目标结点 target 距离为 K 的所有结点的值的列表。 答案可以以任何顺序返回。
示例 1:
输入:root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2
输出:[7,4,1]
解释:
所求结点为与目标结点(值为 5)距离为 2 的结点,
值分别为 7,4,以及 1注意,输入的 "root" 和 "target" 实际上是树上的结点。
上面的输入仅仅是对这些对象进行了序列化描述。
提示:
给定的树是非空的,且最多有 K 个结点。
树上的每个结点都具有唯一的值 0 <= node.val <= 500 。
目标结点 target 是树上的结点。
0 <= K <= 1000.
问题求解:
解法一、
第一种解法是使用Graph + BFS。换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可。
这种解法是比较直观的解法,是必须要进行掌握的,时间复杂度为O(n)。
Map<TreeNode, Set<TreeNode>> graph = new HashMap<>();
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
List<Integer> res = new ArrayList<>();
dfs(root, null);
Queue<TreeNode> q = new LinkedList<>();
Set<TreeNode> used = new HashSet<>();
q.add(target);
used.add(target);
int step = 0;
while (!q.isEmpty() && step <= K) {
int size = q.size();
for (int i = 0; i < size; i++) {
TreeNode curr = q.poll();
if (step == K) res.add(curr.val);
for (TreeNode next : graph.get(curr)) {
if (used.contains(next)) continue;
q.add(next);
used.add(next);
}
}
step += 1;
}
return res;
} private void dfs(TreeNode root, TreeNode parent) {
if (root == null) return;
if (!graph.containsKey(root)) graph.put(root, new HashSet<>());
if (parent != null) {
graph.get(root).add(parent);
graph.get(parent).add(root);
}
dfs(root.left, root);
dfs(root.right, root);
}
解法二、
第二种解法自然就是递归解法了,本题的递归解法还是有点难度的,首先需要计算的是root 到 target的距离,如果距离值正好等于 K,那么就将当前的节点加入res,否则在另一个子树中进行collection。其次如果遍历到target,那么直接对target进行collection。
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
List<Integer> res = new ArrayList<>();
distance(root, target, K, res);
return res;
} private int distance(TreeNode root, TreeNode target, int K, List<Integer> res) {
if (root == null) return -1;
if (root == target) {
collection(target, K, res);
return 0;
} int l = distance(root.left, target, K, res);
int r = distance(root.right, target, K, res); if (l >= 0) {
if (l == K - 1) res.add(root.val);
collection(root.right,K - l - 2, res);
return l + 1;
}
if (r >= 0) {
if (r == K - 1) res.add(root.val);
collection(root.left, K - r - 2, res);
return r + 1;
}
return -1;
} private void collection(TreeNode root, int K, List<Integer> res) {
if (root == null || K < 0) return;
if (K == 0) {
res.add(root.val);
return;
}
collection(root.left, K - 1, res);
collection(root.right, K - 1, res);
}
距离为K的节点 All Nodes Distance K in Binary Tree的更多相关文章
- [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】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- the longest distance of a binary tree
版权声明:欢迎查看本博客.希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735 the longest distance ...
- 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] 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 ...
- 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 ...
- 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 ...
- [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 ...
- [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 ...
随机推荐
- UVM中的regmodel建模(三)
总结一下UVM中的寄存器访问实现: 后门访问通过add_hdl_path命令来添加寄存器路径,并扩展uvm_reg_backdoor基类,定义read与write函数,最后在uvm_reg_block ...
- POJ 1836
刚开始二分写错了 wa了很久 这个二分 的好好想想 #include <iostream> #include<cstdio> #include<string.h> ...
- EditPlus 4.3.2502 中文版已经发布(12月5日更新)
新的版本修复了在之前某版本中键盘 End 键定位位置错误的问题.
- Python中type的用法
目录 描述 语法 用法 type和isinstance Type和Object 描述 python的 type 函数有两个用法,当只有一个参数的时候,返回对象的类型.当有三个参数的时候返回一个类对象. ...
- Linux基础命令---swapon
swapon 在指定的设备上启用交换分区,使用的设备或文件由专用文件参数提供.它可以是”-L label”或”-U UUID”,以指示一个设备的标签或UUID.对swapon的调用通常发生在系统引导脚 ...
- MySQL Crash Course #01# Chapter 1. 2 概念. Primary key
索引 database table schema Primary Key MySQL 书的第一章介绍一些基本的概念.理解数据库是掌握 MySQL 非常重要的一个部分. 第二章简单介绍了 MySQL 以 ...
- P3313 [SDOI2014]旅行
P3313 [SDOI2014]旅行 树链剖分+动态线段树(并不是lct) 显然的,我们对于每一个宗教都要维护一个线段树. (那么空间不是爆炸了吗) 在这里引入:动态开点线段树 就是需要的点开起来,不 ...
- mongodb 最佳可视化工具mongobooster
最好用的mongodb GUI工具 mongobooster,没有之一,可从https://mongobooster.com/下载 常见管理命令可参考,http://www.cnblogs.com/l ...
- 面向对象初调用:foolish 电梯
本周我们完成的任务是傻瓜电梯的调度,对于那十分十分详细的指导书,我感觉想要说明白题目要求,是做不到的,所以就把指导书贴出来给大家看了,,由于在下还不会网页制作,只能通过百度网盘了,https://pa ...
- 奇怪的问题:改变了界面,生成的exe不起作用
现象:修改ui界面的控件,如改变text,生成exe没有改变,清理.重新创建也没有作用 原因:ui.h没有被更新,每次软件生成的时候都是调用的旧的文件 解决:先将debug和release文件夹删除, ...