距离为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 ...
随机推荐
- spring mvc interceptors
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/> <mvc:e ...
- Hive sql和Presto sql的一些对比
最近由于工作上和生活上的一些事儿好久没来博客园了,但是写博客的习惯还是得坚持,新的一年需要更加努力,困知勉行,终身学习,每天都保持空杯心态.废话不说,写一些最近使用到的Presto SQL和Hive ...
- python repr方法和str方法
每个类都有默认的__repr__, __str__方法,用print 实例时调用类的str方法,直接输出类的实例,调用的是类的repr方法 在命令行界面,不用print命令打印而是直接写变量名,就是用 ...
- jquery checkbox相关 prop方法
jquery checkbox相关 prop方法 firefox中 checkbox属性checked="checked"已有,但复选框却不显示打钩的原因复选框绑定了click事件 ...
- MySQL Crash Course #17# Chapter 25. 触发器(Trigger)
推荐看这篇mysql 利用触发器(Trigger)让代码更简单 以及 23.3.1 Trigger Syntax and Examples 感觉有点像 Spring 里的 AOP 我们为什么需要触发器 ...
- P2512 [HAOI2008]糖果传递&&P3156 [CQOI2011]分金币&&P4016 负载平衡问题
P2512 [HAOI2008]糖果传递 第一步,当然是把数据减去平均数,然后我们可以得出一串正负不等的数列 我们用sum数组存该数列的前缀和.注意sum[ n ]=0 假设为链,那么可以得出答案为a ...
- P3435 [POI2006]OKR-Periods of Words
P3435 [POI2006]OKR-Periods of Words 题解传送门 kmp 注意:由于题目说只要A满足是2Q的前缀,所以求的不是严格的最大循环子串(20pts) 我们需要求出的是在主串 ...
- UVA 11426 GCD - Extreme (II) (欧拉函数)题解
思路: 虽然看到题目就想到了用欧拉函数做,但就是不知道怎么做... 当a b互质时GCD(a,b)= 1,由此我们可以推出GCD(k*a,k*b)= k.设ans[i]是1~i-1与i的GCD之和,所 ...
- TeeChart的网络资料
TeeChart坐标轴常见问题 http://www.shaoqun.com/a/54063.aspx TeeChart常用编程语句汇总(C#) http://www.ev get.com/artic ...
- 51nod 1284 2 3 5 7的倍数
从1到N 里 是2的倍数 有 N/2 个 然后大概看过这类的blog 所以运用容斥原理 直接计算 是 2 3 5 7 的个数都是多少 然后用N 减去 就是 不是2 3 5 7 的个数了 (离散好像也 ...
