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 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.

题意是让我们在一颗二叉树中,给定节点 Target, 寻找和target节点距离 为 K的所有节点,我们可以把这颗树看成一个无向图, 没有顺序就意味着,二叉树的旁边的指节也是可以算距离的。
我们可以先把二叉树转化为无向图,再在图中进行 BFS 搜索应该就可以得到答案。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> distanceK(TreeNode root, TreeNode target, int K) { List<Integer> resList = new ArrayList<Integer> ();
resList.add(target.val); Set<Integer> visited = new HashSet<>();
visited.add(target.val); Map<Integer, List<Integer>> graph = new HashMap<>();
treeToGraph(graph, null, root); if(graph.isEmpty() && target == root && K == 0){
return resList;
}
else if(graph.isEmpty()){
return new ArrayList<Integer> ();
} for(int i=0; i<K; i++){
List<Integer> temp = new ArrayList<>();
for(Integer v : resList){
for(Integer e : graph.get(v)){
if(!visited.contains(e)){
visited.add(e);
temp.add(e);
}
}
}
resList = temp;
}
return resList; } public void treeToGraph(Map<Integer, List<Integer>> map, TreeNode parent, TreeNode child){
if(parent != null && child != null){
if(map.containsKey(parent.val)){
map.get(parent.val).add(child.val);
}
else{
List<Integer> list = new ArrayList<>();
list.add(child.val);
map.put(parent.val, list);
}
if(map.containsKey(child.val)){
map.get(child.val).add(parent.val);
}
else{
List<Integer> list = new ArrayList<>();
list.add(parent.val);
map.put(child.val, list);
}
}
if(child.left != null){
treeToGraph(map, child, child.left);
}
if(child.right != null){
treeToGraph(map, child, child.right);
} }
}
LeetCode – All Nodes Distance K in Binary Tree的更多相关文章
- [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】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- 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 ...
- [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 ...
- 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 ...
- [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 ...
- 863. All Nodes Distance K in Binary Tree
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...
- [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 ...
随机推荐
- js中 offset /client /scroll总结
offset家族(只能读取,不能操作): offsetLeft:元素的边框的外边缘距离与已定位的父容器(offsetparent)的左边距离(就是子元素左边框到父元素左边框的距离). offsetTo ...
- flask项目结构(四)使用sqlalchemy和alembic
简介 其实我不是啥正经人,错了,不是啥正经程序员,所能想到的估计也就码农一级吧,高级程序员,搞什么算法,什么人工智能,大数据计算…………离我还太遥远. 但是这并不妨碍我继续学习,继续写垃圾小程序. 反 ...
- Oauth2.0:Access Token 与 Refresh Token
access token 是客户端访问资源服务器的令牌.拥有这个令牌代表着得到用户的授权.然而,这个授权应该是临时的,有一定有效期.这是因为,access token 在使用的过程中可能会泄露.给 a ...
- 怎样在Ubuntu 14.04中安装Java(转)
想知道如何在Ubuntu 14.04中安装Java?安装Java肯定是安装Ubuntu 14.04后首先要做的几件事情之一(见http://www.linuxidc.com/Linux/2014-04 ...
- String 和StringBuffer的简单实用案例
3.现在有个字符串是按照如下格式保存的:“张三:90|李四:80|王五:100” 显示后的数据如下所示,按不同的人员显示: 姓名:张三,成绩是:90: 姓名:李四,成绩是:90: 姓名:王五,成绩是: ...
- HTML5触摸事件(touchstart、touchmove和touchend)
HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事 ...
- C点滴成海------Dev C++怎么修改成简体中文
第一步:选择菜单中的Tools 第二步:选择Tools中的“Envirnoment Options”,即第二个选项 第三步:选择中文并保存 将"1"的语言改成中文就行了
- localStorage的使用记录
// 存数据 var str = JSON.stringify(back); localStorage.setItem("options", str); // 取数据 var op ...
- setcookie
cookie 中值的部分在发送的时候会被自动用 urlencode 编码并在接收到的时候被自动解码并把值赋给与自己同名的 cookie 变量 首先声明,浏览的Cookie操作都是通过HTTP Head ...
- 2019-03-15-day011-递归生成器
函数的进阶: 动态参数: 两种: 动态位置参数 > 动态默认参数 打散(聚合): 实参处打散 形参处聚合 不在函数中第一次使用*打散,第二次是聚合 在数据库中快速写入数据的时候,**dic 名称 ...