[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 ...
随机推荐
- 【转】strlen源码
strlen源码剖析 学习高效编程的有效途径之一就是阅读高手写的源代码,CRT(C/C++ Runtime Library)作为底层的函数库,实现必然高效.恰好手中就有glibc和VC的CRT源代码, ...
- 【分布式系列之ActiveMq】ActiveMq入门示例
前言 github地址:https://github.com/AndyFlower/web-back/tree/master/ActiveMq01 下载ActiveMQ :http://activem ...
- 【VI】如何删除匹配指定字符串的行(已解决)
命令: g/pattern/d 如,删除包含字母 hell 的行 g/hell/d 删除 不 匹配指定字符的行(未验证,有需要的朋友可以试一下) v/pattern/d g!/pattern/d
- 为android编译libsocket的脚本
#!/bin/bash U32=0 #编译64位arm时 U32=0 编译32位arm时 U32=1 其他参数不需要变动 TARGET=android-24 HOST=darwin-x86_64 ...
- sencha touch list tpl 监听组件插件(2013-9-15)
插件代码 /* *list tpl模版加入按钮监控 *<div class="x-button-normal x-button x-iconalign-center x-layout- ...
- Linux "bring up eth0 failed, eth0 seems not be presernt" 问题解决方案
=========1.问题========== 重启网卡的时候出现"bring up eth0 failed, eth0 seems not be presernt", 提示找不到 ...
- 供安全工程师实用的SOC模型
一.背景 如今,安全概念满天飞,什么安全运营中心(SOC).威胁情报(TI).态势感知等等不一而足,这些概念及其背后代表的安全思想都很好,不过很多产品为了迎合国内的工作汇报都做成了很多Dashboar ...
- 浅谈 Java 字符串(String, StringBuffer, StringBuilder)
我们先要记住三者的特征: String 字符串常量 StringBuffer 字符串变量(线程安全) StringBuilder 字符串变量(非线程安全) 一.定义 查看 API 会发现,String ...
- MongoDB 日记参数
MongoDB中主要有四种日志.分别是系统日志.Journal日志.oplog主从日志.慢查询日志等.这些日志记录着Mongodb数据库不同方便的踪迹.下面分别介绍这四种日志: 1.系统日志 系统日志 ...
- JAVAORM框架之Mybatis (Ibatis) 详解
目录 Mybatis基础概念 Mybatis开放方式演进 Mybatis框架核心要点 关联查询 延迟加载(懒加载) 动态SQL Mybatis缓存 Mybatis逆向工程 PageHelper分页插件 ...
