Skip to content

Commit c945d88

Browse files
committed
[0212]ADD:Lc-119
1 parent f11e769 commit c945d88

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

readme.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
[[20210204]1161. 最大层内元素和-Medium](树/1161.%20最大层内元素和-Medium.md)
1111
[[20210212]119. 杨辉三角 II-Easy](动态规划/119.%20杨辉三角%20II-Easy.md)
1212
[[20210212]236. 二叉树的最近公共祖先-Medium](树/236.%20二叉树的最近公共祖先-Medium.md)
13+
[[20210212]863. 二叉树中所有距离为 K 的结点-Medium](回溯法/BFS/863.%20二叉树中所有距离为%20K%20的结点-Medium.md)
1314
## 2021.01
1415
[[20210104]1688. 比赛中的配对次数-Easy](回溯法/1688.%20比赛中的配对次数-Easy.md)
1516
[[20210105]830. 较大分组的位置-Easy](字符串/830.%20较大分组的位置-Easy.md)
@@ -199,6 +200,7 @@
199200
## 回溯法
200201
### BFS
201202
- [752. 打开转盘锁-Medium](回溯法/BFS/752.%20打开转盘锁-Medium.md)
203+
- [863. 二叉树中所有距离为 K 的结点-Medium](回溯法/BFS/863.%20二叉树中所有距离为%20K%20的结点-Medium.md)
202204

203205
[39. 组合总和-Medium](回溯法/39.%20组合总和-Medium.md)
204206
[46. 全排列-Medium](回溯法/46.%20全排列-Medium.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# [Description](https://leetcode-cn.com/problems/all-nodes-distance-k-in-binary-tree)
2+
3+
定一个二叉树(具有根结点 root), 一个目标结点 target ,和一个整数值 K 。
4+
5+
返回到目标结点 target 距离为 K 的所有结点的值的列表。 答案可以以任何顺序返回。
6+
7+
 
8+
9+
示例 1:
10+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png)
11+
```python
12+
输入:root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, K = 2
13+
输出:[7,4,1]
14+
解释:
15+
所求结点为与目标结点(值为 5)距离为 2 的结点,
16+
值分别为 74,以及 1
17+
18+
注意,输入的 "root""target" 实际上是树上的结点。
19+
上面的输入仅仅是对这些对象进行了序列化描述。
20+
```
21+
22+
提示:
23+
24+
- 给定的树是非空的。
25+
- 树上的每个结点都具有唯一的值 0 <= node.val <= 500 。
26+
- 目标结点 target 是树上的结点。
27+
- 0 <= K <= 1000.
28+
29+
30+
# Solution
31+
- 步骤1:将树改造,找到target节点,并以该节点为新树1;将target的父节点作为根节点,构建新树2,因此该路径上的节点的父子关系均需改变(由self.dfs()完成)
32+
- 步骤2:层序遍历两棵新树,得到结果
33+
![image.png](https://pic.leetcode-cn.com/3592d875064a80da2b9e605d3eaba4b2f8689512c2db246985094c83892c194b-image.png)
34+
```python
35+
class Solution:
36+
def distanceK(self, root: TreeNode, target: TreeNode, K: int) -> List[int]:
37+
if not root:
38+
return []
39+
40+
self.ans = []
41+
self.new_root = None
42+
self.dfs(root, target, None)
43+
44+
self.collect(target, K)
45+
self.collect(self.new_root, K-1)
46+
47+
return self.ans
48+
49+
def dfs(self, root, target, father):
50+
if not root:
51+
return None
52+
if target == root:
53+
self.new_root = father
54+
return True
55+
56+
left = self.dfs(root.left, target, root)
57+
right = self.dfs(root.right, target, root)
58+
59+
if left:
60+
root.left = father
61+
return True
62+
63+
if right:
64+
root.right = father
65+
return True
66+
67+
return False
68+
69+
def collect(self, root, target_level):
70+
if not root:
71+
return
72+
73+
step = 0
74+
queue = [root]
75+
76+
while queue:
77+
for i in range(len(queue)):
78+
tmp = queue.pop(0)
79+
if step == target_level:
80+
self.ans.append(tmp.val)
81+
82+
if tmp.left:
83+
queue.append(tmp.left)
84+
if tmp.right:
85+
queue.append(tmp.right)
86+
87+
step += 1
88+
```

0 commit comments

Comments
 (0)