[抄题]:

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.

[暴力解法]:

时间分析:

空间分析:

[优化后]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

没啥思路,以为是dfs就可以了。但是其实分为两步:

存储:把root,左右长度存在map中

取出来:length每次加一,递增为k

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. map中存的距离是找出left之后,left+1
  2. 在map中找root之前要先判断是否有这个key

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

先在左边右边用dfs生成长度,存map。再用dfs的length+1找出所有节点

[复杂度]:Time complexity: O(n) Space complexity: O(n)

[算法思想:迭代/递归/分治/贪心]:

[关键模板化代码]:

int left = find(root.left, target, K);
if (left >= 0) {
map.put(root, left + 1);
return left + 1;
}

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

[是否头一次写此类driver funcion的代码] :

[潜台词] :

class Solution {
//initialization: hashmap
HashMap<TreeNode, Integer> map = new HashMap<TreeNode, Integer>(); public List<Integer> distanceK(TreeNode root, TreeNode target, int K) {
List<Integer> result = new ArrayList<Integer>();
find(root, target, K);
dfs(root, 0, target, K, result);
return result;
} //find and store length
public int find(TreeNode root, TreeNode target, int K) {
//corner case: root == null
if (root == null) return -1; //root.val == target
if (target == root) {
map.put(root, 0);
return 0;
} //define left, right and add
int left = find(root.left, target, K);
if (left >= 0) {
map.put(root, left + 1);
return left + 1;
}
int right = find(root.right, target, K);
if (right >= 0) {
map.put(root, right + 1);
return right + 1;
} return -1;
} //add the points to res
public void dfs(TreeNode root, int length, TreeNode target, int K, List<Integer> res) {
//corner case
if (root == null) return ; //get length if there is key
if (map.containsKey(root)) {
length = map.get(root);
}
//add to res
if (length == K) res.add(root.val); //dfs in left and right
dfs(root.left, length + 1, target, K, res);
dfs(root.right, length + 1, target, K, res);
}
}

863. All Nodes Distance K in Binary Tree 到制定节点距离为k的节点的更多相关文章

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

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

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

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

  4. 距离为K的节点 All Nodes Distance K in Binary Tree

    2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...

  5. the longest distance of a binary tree

    版权声明:欢迎查看本博客.希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735 the longest distance ...

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

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

  8. 863. All Nodes Distance K in Binary Tree

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

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

随机推荐

  1. PythonStudy——可变与不可变 Variable and immutable

    ls = [10, 20, 30] print(id(ls), ls) ls[0] = 100 print(id(ls), ls) print(id(10)) print(id(20)) print( ...

  2. liunx学习笔记(一:常用命令)

    linux: 在学习linux之前我们应该多少了解windows的一些相关操作,linux也就是类似windows的另一种操作系统,用来管理软硬件的一种应用.在windows下你可以通过鼠标点击相关的 ...

  3. 项目管理软件 GanttProject 节日表

    项目管理软件 GanttProject 节日表 GanttProject 是一款项目管理软件,对比一些商业项目管理软件一点不差,有些还更强大,比如以下的节假日自动选择. 文件地址在:C:\Progra ...

  4. crontab定时任务第一个周期未完成下一个周期执行就来了

    一.现象 有一个定时任务,每分钟执行一次,前一分钟还没有执行完成,下一个分钟就来了,怎么解决. 二.模拟 #!/bin/bash echo "开始 `date` " >> ...

  5. ubuntu16.04下docker安装和简单使用

    前提条件 操作系统 docker-ce支持的ubuntu版本: Bionic 18.04 (LTS) Xenial 16.04 (LTS) Trusty 14.04 (LTS) 卸载旧版本docker ...

  6. sqlserver 带输出参数的存储过程的创建与执行

    创建 use StudentManager go if exists(select * from sysobjects where name='usp_ScoreQuery4') drop proce ...

  7. React.js 学习总结

    1.用React的优越性 独立,小巧,快速 ReactDOM 会构建一个虚拟的DOM tree, 只有DOM tree 的某个部分发生变化,便重新渲染某个部分. React 判断DOM 是否重新渲染的 ...

  8. 9-安装redis

    1.在linux上安装C语言环境 yum install gcc-c++ 2.解压源码包 tar -xvf /opt/soft/redis-3.0.0.tar -C /opt/app/ 3.编译源码( ...

  9. Android 开发 创建WiFi、WiFi热点 ---开发集合

    WIFI 权限 <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" /> < ...

  10. python:win下将py文件打包成exe

    [环境]windows,正常运行的python文件 1.安装pyinstaller ,cmd下执行以下命令,需看到安装成功界面 pip install pyinstaller 2.cmd中进入要打包的 ...