Skip to content

Commit 91f05b7

Browse files
committed
+ problem 863
1 parent a629981 commit 91f05b7

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 863. All Nodes Distance K in Binary Tree
2+
Given the `root` of a binary tree, the value of a target node `target`, and an integer `k`, return *an array of the values of all nodes that have a distance* `k` *from the target node*.
3+
4+
You can return the answer in **any order**.
5+
6+
#### Example 1:
7+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png)
8+
<pre>
9+
<strong>Input:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
10+
<strong>Output:</strong> [7,4,1]
11+
<strong>Explanation:</strong> The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
12+
</pre>
13+
14+
#### Example 2:
15+
<pre>
16+
<strong>Input:</strong> root = [1], target = 1, k = 3
17+
<strong>Output:</strong> []
18+
</pre>
19+
20+
#### Constraints:
21+
* The number of nodes in the tree is in the range `[1, 500]`.
22+
* `0 <= Node.val <= 500`
23+
* All the values `Node.val` are **unique**.
24+
* `target` is the value of one of the nodes in the tree.
25+
* `0 <= k <= 1000`
26+
27+
## Solutions (Python)
28+
29+
### 1. Solution
30+
```Python
31+
# Definition for a binary tree node.
32+
# class TreeNode:
33+
# def __init__(self, x):
34+
# self.val = x
35+
# self.left = None
36+
# self.right = None
37+
38+
class Solution:
39+
def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
40+
root.parent = None
41+
stack = [root]
42+
deque = collections.deque([(target, 0)])
43+
ret = []
44+
45+
while stack != []:
46+
node = stack.pop()
47+
if node.left is not None:
48+
node.left.parent = node
49+
stack.append(node.left)
50+
if node.right is not None:
51+
node.right.parent = node
52+
stack.append(node.right)
53+
54+
while len(deque) > 0:
55+
node, d = deque.popleft()
56+
57+
if d > k:
58+
break
59+
elif d == k:
60+
ret.append(node.val)
61+
62+
if node.left is not None:
63+
deque.append((node.left, d + 1))
64+
node.left.parent = None
65+
if node.right is not None:
66+
deque.append((node.right, d + 1))
67+
node.right.parent = None
68+
if node.parent is not None:
69+
deque.append((node.parent, d + 1))
70+
if node == node.parent.left:
71+
node.parent.left = None
72+
else:
73+
node.parent.right = None
74+
75+
return ret
76+
```
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 863. 二叉树中所有距离为 K 的结点
2+
给定一个二叉树(具有根结点 `root`), 一个目标结点 `target` ,和一个整数值 `k` ,返回到目标结点 `target` 距离为 `k` 的所有结点的值的数组。
3+
4+
答案可以以 **任何顺序** 返回。
5+
6+
#### 示例 1:
7+
![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/06/28/sketch0.png)
8+
<pre>
9+
<strong>输入:</strong> root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
10+
<strong>输出:</strong> [7,4,1]
11+
<strong>解释:</strong> 所求结点为与目标结点(值为 5)距离为 2 的结点,值分别为 7,4,以及 1
12+
</pre>
13+
14+
#### 示例 2:
15+
<pre>
16+
<strong>输入:</strong> root = [1], target = 1, k = 3
17+
<strong>输出:</strong> []
18+
</pre>
19+
20+
#### 提示:
21+
* 节点数在 `[1, 500]` 范围内
22+
* `0 <= Node.val <= 500`
23+
* `Node.val` 中所有值 **不同**
24+
* 目标结点 `target` 是树上的结点。
25+
* `0 <= k <= 1000`
26+
27+
## 题解 (Python)
28+
29+
### 1. 题解
30+
```Python
31+
# Definition for a binary tree node.
32+
# class TreeNode:
33+
# def __init__(self, x):
34+
# self.val = x
35+
# self.left = None
36+
# self.right = None
37+
38+
class Solution:
39+
def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
40+
root.parent = None
41+
stack = [root]
42+
deque = collections.deque([(target, 0)])
43+
ret = []
44+
45+
while stack != []:
46+
node = stack.pop()
47+
if node.left is not None:
48+
node.left.parent = node
49+
stack.append(node.left)
50+
if node.right is not None:
51+
node.right.parent = node
52+
stack.append(node.right)
53+
54+
while len(deque) > 0:
55+
node, d = deque.popleft()
56+
57+
if d > k:
58+
break
59+
elif d == k:
60+
ret.append(node.val)
61+
62+
if node.left is not None:
63+
deque.append((node.left, d + 1))
64+
node.left.parent = None
65+
if node.right is not None:
66+
deque.append((node.right, d + 1))
67+
node.right.parent = None
68+
if node.parent is not None:
69+
deque.append((node.parent, d + 1))
70+
if node == node.parent.left:
71+
node.parent.left = None
72+
else:
73+
node.parent.right = None
74+
75+
return ret
76+
```
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
8+
class Solution:
9+
def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
10+
root.parent = None
11+
stack = [root]
12+
deque = collections.deque([(target, 0)])
13+
ret = []
14+
15+
while stack != []:
16+
node = stack.pop()
17+
if node.left is not None:
18+
node.left.parent = node
19+
stack.append(node.left)
20+
if node.right is not None:
21+
node.right.parent = node
22+
stack.append(node.right)
23+
24+
while len(deque) > 0:
25+
node, d = deque.popleft()
26+
27+
if d > k:
28+
break
29+
elif d == k:
30+
ret.append(node.val)
31+
32+
if node.left is not None:
33+
deque.append((node.left, d + 1))
34+
node.left.parent = None
35+
if node.right is not None:
36+
deque.append((node.right, d + 1))
37+
node.right.parent = None
38+
if node.parent is not None:
39+
deque.append((node.parent, d + 1))
40+
if node == node.parent.left:
41+
node.parent.left = None
42+
else:
43+
node.parent.right = None
44+
45+
return ret

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@
544544
[859][859l] |[Buddy Strings][859] |![py]
545545
[860][860l] |[Lemonade Change][860] |![rs]
546546
[861][861l] |[Score After Flipping Matrix][861] |![rs]
547+
[863][863l] |[All Nodes Distance K in Binary Tree][863] |![py]
547548
[865][865l] |[Smallest Subtree with all the Deepest Nodes][865] |![py]
548549
[866][866l] |[Prime Palindrome][866] |![rs]
549550
[867][867l] |[Transpose Matrix][867] |![py]
@@ -2081,6 +2082,7 @@
20812082
[859]:Problemset/0859-Buddy%20Strings/README.md#859-buddy-strings
20822083
[860]:Problemset/0860-Lemonade%20Change/README.md#860-lemonade-change
20832084
[861]:Problemset/0861-Score%20After%20Flipping%20Matrix/README.md#861-score-after-flipping-matrix
2085+
[863]:Problemset/0863-All%20Nodes%20Distance%20K%20in%20Binary%20Tree/README.md#863-all-nodes-distance-k-in-binary-tree
20842086
[865]:Problemset/0865-Smallest%20Subtree%20with%20all%20the%20Deepest%20Nodes/README.md#865-smallest-subtree-with-all-the-deepest-nodes
20852087
[866]:Problemset/0866-Prime%20Palindrome/README.md#866-prime-palindrome
20862088
[867]:Problemset/0867-Transpose%20Matrix/README.md#867-transpose-matrix
@@ -3617,6 +3619,7 @@
36173619
[859l]:https://leetcode.com/problems/buddy-strings/
36183620
[860l]:https://leetcode.com/problems/lemonade-change/
36193621
[861l]:https://leetcode.com/problems/score-after-flipping-matrix/
3622+
[863l]:https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/
36203623
[865l]:https://leetcode.com/problems/smallest-subtree-with-all-the-deepest-nodes/
36213624
[866l]:https://leetcode.com/problems/prime-palindrome/
36223625
[867l]:https://leetcode.com/problems/transpose-matrix/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@
544544
[859][859l] |[亲密字符串][859] |![py]
545545
[860][860l] |[柠檬水找零][860] |![rs]
546546
[861][861l] |[翻转矩阵后的得分][861] |![rs]
547+
[863][863l] |[二叉树中所有距离为 K 的结点][863] |![py]
547548
[865][865l] |[具有所有最深结点的最小子树][865] |![py]
548549
[866][866l] |[回文素数][866] |![rs]
549550
[867][867l] |[转置矩阵][867] |![py]
@@ -2081,6 +2082,7 @@
20812082
[859]:Problemset/0859-Buddy%20Strings/README_CN.md#859-亲密字符串
20822083
[860]:Problemset/0860-Lemonade%20Change/README_CN.md#860-柠檬水找零
20832084
[861]:Problemset/0861-Score%20After%20Flipping%20Matrix/README_CN.md#861-翻转矩阵后的得分
2085+
[863]:Problemset/0863-All%20Nodes%20Distance%20K%20in%20Binary%20Tree/README_CN.md#863-二叉树中所有距离为-k-的结点
20842086
[865]:Problemset/0865-Smallest%20Subtree%20with%20all%20the%20Deepest%20Nodes/README_CN.md#865-具有所有最深结点的最小子树
20852087
[866]:Problemset/0866-Prime%20Palindrome/README_CN.md#866-回文素数
20862088
[867]:Problemset/0867-Transpose%20Matrix/README_CN.md#867-转置矩阵
@@ -3617,6 +3619,7 @@
36173619
[859l]:https://leetcode.cn/problems/buddy-strings/
36183620
[860l]:https://leetcode.cn/problems/lemonade-change/
36193621
[861l]:https://leetcode.cn/problems/score-after-flipping-matrix/
3622+
[863l]:https://leetcode.cn/problems/all-nodes-distance-k-in-binary-tree/
36203623
[865l]:https://leetcode.cn/problems/smallest-subtree-with-all-the-deepest-nodes/
36213624
[866l]:https://leetcode.cn/problems/prime-palindrome/
36223625
[867l]:https://leetcode.cn/problems/transpose-matrix/

0 commit comments

Comments
 (0)