[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 ...
随机推荐
- 【大数据系列】HDFS安全模式
一.什么是安全模式 安全模式时HDFS所处的一种特殊状态,在这种状态下,文件系统只接受读数据请求,而不接受删除.修改等变更请求.在NameNode主节点启动时,HDFS首先进入安全模式,DataNod ...
- HDFS的客户端操作
命令行操作: -help 功能:输出这个命令参数手册 -ls 功能:显示目录信息 示例: hadoop fs -ls hdfs://hadoop-serv ...
- 对Array进行排序(按字母顺序)
通过使用java.utils.Arrays.sort()和String.CASE_INSENSITIVE_OREDR,可以很容易的对结果进行排序(按字母顺序): String[] array = { ...
- Android 屏幕适配:最全面的解决方案
转自:https://www.jianshu.com/p/ec5a1a30694b 前言 Android的屏幕适配一直以来都在折磨着我们Android开发者,本文将结合: Google的官方权威适配文 ...
- 题目1448:Legal or Not(有向无环图判断——拓扑排序问题)
题目链接:http://ac.jobdu.com/problem.php?pid=1448 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- angularjs实现选项卡实例
注意:事件.循环.赋值在一起就出错 错误实例: <!DOCTYPE html> <html ng-app="tab_switch"> <head> ...
- HDU 2199 Can you solve this equation(二分答案)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- 遮挡剔除 Occlusion Culling(转)
一.首先介绍下draw call(这个东西越少你的游戏跑的越快): 在游戏中每一个被展示的独立的部分都被放在了一个特别的包中,我们称之为“描绘指令”(draw call),然后这个包传递到3D部分在屏 ...
- Unity3D笔记 愤怒的小鸟<五> 小鸟动画+Unity3D如何设置断点调式
前言:实现小鸟的动画,之前吐槽过js写U3D,就改成了C#来写,没想到遇到问题了. 实现的效果 using UnityEngine; using System.Collections; /// < ...
- layer.load()加载层如何加入文字描述
https://fly.layui.com/jie/3586/ https://www.layui.com/doc/modules/layer.html#layer.load //loading层va ...
