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
.
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
target
node is a node in the tree. 0 <= K <= 1000
.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
没啥思路,以为是dfs就可以了。但是其实分为两步:
存储:把root,左右长度存在map中
取出来:length每次加一,递增为k
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- map中存的距离是找出left之后,left+1
- 在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的节点的更多相关文章
- 【LeetCode】863. All Nodes Distance K in Binary Tree 解题报告(Python)
[LeetCode]863. All Nodes Distance K in Binary Tree 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http ...
- [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 ...
- 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 ...
- 距离为K的节点 All Nodes Distance K in Binary Tree
2018-07-26 17:38:37 问题描述: 问题求解: 解法一. 第一种解法是使用Graph + BFS.换言之,就是将二叉树转化为无向图,然后在无向图中使用BFS进行层次遍历即可. 这种解法 ...
- the longest distance of a binary tree
版权声明:欢迎查看本博客.希望对你有有所帮助 https://blog.csdn.net/cqs_2012/article/details/24880735 the longest distance ...
- 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 ...
- [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] 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 ...
随机推荐
- hnsdfz -- 6.19 -- day4
感觉还好…… 暴力分挂了很多不知道为什么…… 听说今天出题人hsh很劲…… c题正解是个奇怪的知识点…… 恩总的来说今天的节奏依旧很散(大课间去围观sdfz跑操了233 暴力分都写了但是似乎没有尝试脑 ...
- IDEA一定要改的八条配置
引言 坦白说,我很少写这种操作类型的文章.因为这种文章没啥新意,大家操作步骤肯定是一样的.然而,我答应了我的同事小阳,给她出一篇!毕竟人家打算从Eclipse转IDEA了,于是以示鼓励,写一篇给她! ...
- laravel 路由分組
laravel 路由分組 Route::group(['prefix' => 'admin'], function () { $namespacePrefix="\\App\\Http ...
- centos7怎么查看、打开和关闭防火墙
使用centos7会发现,用centos6以前的方式查看.打开和关闭防火墙都无效了.这是因为centos7的防火墙改用firewalld,而不再使用iptables了 查看centos7的防火墙的状态 ...
- Python完全新手教程
转发:作者: taowen 来源: 博客园 发布时间: 2010-10-01 00:42 阅读: 1618 次 推荐: 0 原文链接 [收藏] Lesson ...
- flex 布局压缩问题
在 flex 布局中,当有一个元素宽度过长时,另一个元素宽度会被压缩, 如下图: 解决办法:在不想被压缩的元素上加上样式 flex-shrink: 0; 效果图:
- Java12配置
配置环境变量: 之前的JAVA_HOME和CLASSPATH已经都不要了.只要配置jdk的bin到Path里: C:\Program Files\Java\jdk-12\bin
- 黄聪:用 CSS 实现元素垂直居中,有哪些好的方案?
1.不知道自己高度和父容器高度的情况下, 利用绝对定位只需要以下三行: parentElement{ position:relative; } childElement{ position: abso ...
- Linux文件误删之后恢复方法
前言 今天不小心把一个文件给误删了,因为不想花半天时间重新写,就查找了一下Linux下恢复文件的方法. 因为是刚删不久,文件实际的数据应该还在 首先查看系统分区 Linux:~# df Filesys ...
- [UE4]Window Title Bar Area
一.Window Title Bar Area.windows窗口拖拽控件 二.window Buttons Enabled,在控件的右上角显示:最小化.最大化,关闭按钮; Toggle Fullsc ...