[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 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
targetnode is a node in the tree. 0 <= K <= 1000.
经典的BFS, 其实跟之前做的employee importance很像, 只不过这个是tree, 那个是已经做好的列表, 但是我们可以用helper函数来讲tree 转换为一个undirected graph, 就是一个dictionary, 然后就用经典的BFS, 只不过append queue时, 要append进去id 和当前的distance. 注意一点的是edge case, k == 0, return [target.val], 其他时候是不包括target.val的.不过因为最开始visited已经有了target.val, 所以好像可以将判断node != target.val去掉. 经过验证, 确实是不需要这一步了.
1. constraints
1) tree not empty
2) node.val is unique
3) target always in tree
4) 0 <= K <= 1000, edge case, K== 0 , 直接return target.val
2. ideas
BFS : T: O(n) S: O(n) n: number of nodes
1. edge
2. 将tree 转换为dictionary
3. 用常规的BFS的做法, 只不过append queue时, 要append进去id 和当前的distance.
3. code
class Solution:
def distancek(self, root, target, K):
if K == 0: return [target.val]
helper(root):
if root:
if root.left:
d[root.val].add(root.left.val)
d[root.left.val].add(root.val)
helper(root.left)
if root.right:
d[root.val].add(root.right.val)
d[root.right.val].add(root.val)
helper(root.right)
d= collections.defaultdict(set)
helper(root)
queue, visited, ans = collections.deque([(target.val, 0)]), set([target.val]), [] # notice 不能直接用set(target.val), 因为要用iterable的parameter
while queue:
node, dis = queue.popleft()
if dis == K:
ans.append(node)
elif dis < K:
for each in d[node]:
if each not in visited:
queue.append((each, dis+1))
visited.add(each)
return ans
4. test cases
用例子里面的cases即可.
[Leetcode] 863. All Nodes Distance K in Binary Tree_ Medium tag: BFS, Amazon的更多相关文章
- 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 解题报告(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 ...
- [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 ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [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 ...
- [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 ...
随机推荐
- 跟bWAPP学WEB安全(PHP代码)--OS命令注入
背景 这是温故知新的一个系列,也是重新拾起WEB安全的一个系列,同时希望能稍微有点对初学者的帮助.第一篇先来讲讲OS命令注入 bWAPP里面有两个页面也就是两个漏洞,来验证OS命令注入.一个是有回显的 ...
- U盘安装Centos7.1操作系统的问题记录
需要的软硬件环境>>>>>>>>>>>>>>>>>1.服务器(笔者用的笔记本).U盘2.Cento ...
- Linux批量杀死进程
杀死进程在linux中使用kill命令了,我们可以下面来给各位介绍一篇关于Linux下批量杀死进程的例子,希望此例子可以对各位同学带来帮助的哦. 批量杀死包含关键字“php-fpm”的进程. kill ...
- Java虚拟机七 虚拟机监控
jstack 用于导出Java应用程序的线程堆栈:jstack [-l] <pid> -l 选项用于打印锁的附加信息 jstack -l 2348 > /data/deadlock. ...
- mac设置文件权限问题
在使用mac时,经常我们遇到相关文件不能使用的情况,其实大多数情况都是,文件权限问题. 文件或目录的访问权限分为只读,只写和可执行三种.以文件为例,只读权限表示只允许读其内容,而禁止对其做任何的更改操 ...
- 关于python爬虫的编码错误
现在才发现很多错误只有自己一点点的去尝试才能发现.不管之前别人怎么和你说,总是不可能面面俱到,所以提升自己的方法就是亲手实践,自己一点点的去发现问题,并一个个的解决.慢慢享受其中无言的快感. 今天就发 ...
- jsp页面获取参数的方法(url解析、el表达式赋值、session取值)【原创】
最近使用myEclispse做网站,使用jsp+js+css做页面,网站中常用到从列表进入详情页面的跳转,下面对详情页面的值填充方式做一个简单总结: 1.url中使用request获取参数 jsp上方 ...
- js深浅copy
...点copy是浅拷贝var obj1 = [1,{a: 1}];//var obj2 = Object.assign( {}, obj1);//浅copy//var obj2 = JSON.par ...
- yum provides "*/nmcli" and apt-get
一般来说著名的linux系统基本上分两大类: 1.RedHat系列:Redhat.Centos.Fedora等 2.Debian系列:Debian.Ubuntu等 RedHat 系列 1 常见的安装包 ...
- nautilus
在~/.bashrc中定义命令别名,添加以下命令: # some more nautilus aliases alias here='nautilus . > /dev/null 2>&a ...
