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:

  1. The given tree is non-empty.
  2. Each node in the tree has unique values 0 <= node.val <= 500.
  3. The target node is a node in the tree.
  4. 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的更多相关文章

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

  2. 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)

    [LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...

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

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

  5. 863. All Nodes Distance K in Binary Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

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

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

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

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

随机推荐

  1. Qt编写带频谱的音乐播放器

    之前有个项目需要将音频文件的频谱显示出来,想了很多办法,后面发现fmod这个好东西,还是跨平台的,就一个头文件+一个库文件就行,简单小巧功能强大,人家做的真牛逼.为了不卡住界面,采用了多线程处理. 可 ...

  2. 【JavaScript】如何判断一个对象是未定义的?(已解决)

    JavaScript中,如果使用了一个未定义的变量,会有这样的错误提示:XXX未定义. 代码中,怎样才能判定一个对象是否定义了呢? 使用  typeof 示例: if("undefined& ...

  3. Eclipse 创建和读取yaml文件

    工具和用法: 1. eclipse插件包:org.dadacoalition.yedit_1.0.20.201509041456-RELEASE.jar 用法:将此jar包复制到eclipse-jee ...

  4. mac下升级terminal/终端的subversion版本方法

    雖然現在程式碼管理已經以 Git 為主了,不過偶爾需要維護一些舊案子還是會用 SVN,懶得轉了. Mac OS 本身有內建 SVN,不過卻是 1.6 版,最近修改一個舊案子就有碰到 project 已 ...

  5. syslinux 和 grub

    syslinux是一个功能强大的引导加载程序,而且兼容各种介质.它的目的是简化首次安装Linux的时间,并建立修护或其它特殊用途的启动盘.它的安装很简单,一旦安装syslinux好之后,sysLinu ...

  6. Web Uploader在低版本IE下无法显示Flash的一种情况

    用户反馈在IE 8下无法正常显示Web Uploader控件,并已安装了Flash插件.调试发现在内部抛出了Runtime Error的错误,关键代码如下: Runtime.create = func ...

  7. ASP.NET Request.Cookies获取某个Cookie的奇怪问题

    公司的某个产品依赖一个Cookie的值,发现在某些情况下即使Request附带了该Cookie(通过Fiddler2监控),服务器端通过HttpContext的Request.Cookies访问该Co ...

  8. 使用eclipse执行maven-release-plugin插件发布jar异常问题(.project)(Cannot prepare the release because you have local modifications )

    开发是用的eclipse,里面有工程文件.project这种文件,运行release:prepare的时候报异常: Cannot prepare the release because you hav ...

  9. Thinkphp框架下(同服务器下)不同二级域名之间session互通共享设置

    在Thinkphp框架下根目录打开index.php 在头部加入如下代码即可: //入口文件 define('DOMAIN','abc.com');//abc.com换成自己的跟域名 //以下两行是为 ...

  10. jfinal的model和record如何相互转化?

    一.model转record: Model类: 1. /** * Convert model to record. */public Record toRecord() { return new Re ...