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 ...
随机推荐
- Mysql 行存储的文件格式
一.Mysql行存储的文件格式概述 InnoDB存储引擎有两种文件格式 Antelope:compact与redundant两种行记录格式 Barracuda:compress与dynamic两种行记 ...
- URL组成成分及各部分作用简介及urllib.parse / uri
URL的一般格式为(带方括号[]的为可选项): protocol :// hostname[:port] / path / [;parameters][?query]#fragment urllib. ...
- 自动化测试工具Telerik Test Studio发布R1 2019|附下载
Telerik Test Studio是一个用于功能性Web.桌面和移动测试的直观测试自动化工具,它能轻松地实现自动化测试.同时会为GUI.性能.加载和API测试提供完整的自动化测试解决方案. [Te ...
- mvc4使用KindEditor文本编辑器
最近做项目要用文本编辑器,编辑器好多种,这里介绍KindEditor在asp.net mvc4中的使用方法. 一.准备工作: 1.下载KindEditor.去官网:http://www.kindsof ...
- html 相对路径 问题
在jsp跳转servlet和servlet跳转jsp过程中,因为servlet和jsp在不同的目录下,所以直接跳转失败.下面是查阅网上的资料,简单的总结下相对路径的问题. 这种情况下index.jsp ...
- day 51
一 window对象 window 对象表示一个浏览器窗口. 在客户端 JavaScript 中,Window 对象是全局对象,所有的表达式都在当前的环境中计算.也就是说,要引用当前窗口根本不需要特殊 ...
- nginx 更改用户组
为什么要更改 Nginx 服务的默认用户:就像更改 ssh 的默认 22 端口一样,增加安全性,Nginx 服务的默认用户是 nobody ,我们更改为 nginx 1) 添加 nginx 用户 us ...
- snmp 简单网管协议
snmpget是取具体的OID的值.(适用于OID值是一个叶子节点的情况) snmpwalk snmpwalk — Fetch all the SNMP objects from an agent & ...
- python操作sqlite3的几项问题分析
不同数据库还是有各自特点的,之前自以为熟悉mysql,然后全都照搬到sqlite3上,这样果然是不行的.笔者就近期在使用sqlite3时碰到的问题做了总结分析,并给出相应解决方法,供大家参考. 1.如 ...
- c#帮助文档chm打不开的问题
c# 帮助文档,chm 格式, 不可以放在含有字符 # 的文件夹下(包括当前文件夹和上级文件夹),文件名也不可以含有 # 字符, 否则会打不开.