Skip to content

Commit b5bb768

Browse files
committed
add 863
1 parent df6df6e commit b5bb768

8 files changed

+244
-65
lines changed

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
3737
| [16%] | [leetcode-discuss.md](./list/leetcode-discuss.md) | 9/54 | - |
3838
| [22%] | [leetcode-contest.md](./list/leetcode-contest.md) | 16/72 | - |
3939
| [45%] | [leetcode-75.md](./list/leetcode-75.md) | 34/75 | - |
40-
| [45%] | [leetcode-google.md](./list/leetcode-google.md) | 212/471 | 2 vips |
41-
| [50%] | [leetcode-topics.md](./list/leetcode-topics.md) | 72/144 | - |
40+
| [45%] | [leetcode-google.md](./list/leetcode-google.md) | 214/471 | 2 vips |
41+
| [50%] | [leetcode-topics.md](./list/leetcode-topics.md) | 73/144 | - |
4242
| [86%] | [leetcode-top-interview-150.md](./list/leetcode-top-interview-150.md) | 130/150 | - |
4343
| [89%] | [neetcode150.md](./list/neetcode150.md) | 134/150 | 1 vip |
44-
| [95%] | [grind169.md](./list/grind169.md) | 161/169 | 4 vips |
44+
| [96%] | [grind169.md](./list/grind169.md) | 163/169 | 4 vips |
4545
| [] | [9c-advanced.md](./list/9c-advanced.md) | 75/93 | 18 vips |
4646
| [] | [9c-dp.md](./list/9c-dp.md) | 42/45 | 3 vips |
4747
| [] | [blind75.md](./list/blind75.md) | 76/77 | 1 vip |
@@ -53,7 +53,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5353
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/129 | 3 vips |
5454
| [🔲] | [9c-top.md](./list/9c-top.md) | 32/54 | 1 vip |
5555

56-
**Solved**: 491 problems
56+
**Solved**: 492 problems
5757

5858
## 类型/Category
5959

@@ -184,7 +184,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
184184
| [Leetcode-874](https://leetcode.com/problems/walking-robot-simulation/) | Walking Robot Simulation | [c++](./leetcode/874.walking-robot-simulation.cpp), [python3](./leetcode/874.walking-robot-simulation.py) | Simulation | O\(N\) | O\(K\) | - |
185185
| [Lintcode-849](https://www.lintcode.com/problem/basic-calculator-iii/) | Basic Calculator III | [c++](./lintcode/849.basic-calculator-iii.cpp), [python3](./lintcode/849.basic-calculator-iii.py) | Simulation | O\(N\) | O\(1\) | Leetcode-772 |
186186
| [Lintcode-553](https://www.lintcode.com/problem/bomb-enemy/) | Bomb Enemy | [c++](./lintcode/553.bomb-enemy.cpp), [python3](./lintcode/553.bomb-enemy.py) | Simulation | O\(NM\) | O\(NM\) | Leetcode-361 |
187-
| [Lintcode-3662](https://www.lintcode.com/problem/design-hit-counter/) | Design Hit Counter | [c++](./lintcode/3662.design-hit-counter.cpp), [python3](./lintcode/3662.design-hit-counter.py) | Simulation | O\(N\) | O\(N\) | - |
187+
| [Lintcode-3662](https://www.lintcode.com/problem/design-hit-counter/) | Design Hit Counter | [c++](./lintcode/3662.design-hit-counter.cpp), [python3](./lintcode/3662.design-hit-counter.py) | Simulation | O\(N\) | O\(N\) | Leetcode-362 |
188188
| [Lintcode-366](https://www.lintcode.com/problem/fibonacci/) | Fibonacci | [c++](./lintcode/366.fibonacci.cpp), [python3](./lintcode/366.fibonacci.py) | Simulation | O\(N\) | O\(N\) | - |
189189
| [Lintcode-645](https://www.lintcode.com/problem/find-the-celebrity/) | Find The Celebrity | [c++](./lintcode/645.find-the-celebrity.cpp), [python3](./lintcode/645.find-the-celebrity.py) | Simulation | O\(N\) | O\(N\) | Leetcode-277 |
190190
| [Lintcode-22](https://www.lintcode.com/problem/flatten-list/) | Flatten List | [c++](./lintcode/22.flatten-list.cpp), [python3](./lintcode/22.flatten-list.py) | Simulation | O\(N\) | O\(N\) | - |
@@ -415,9 +415,10 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
415415

416416
## Hash Table
417417

418-
| Link | Problem(57) | Solution | Tag | Time | Space | Ref |
418+
| Link | Problem(58) | Solution | Tag | Time | Space | Ref |
419419
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
420420
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Hash Table | O\(MlogM\) | O\(N\) | - |
421+
| [Leetcode-863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | All Nodes Distance K In Binary Tree | [c++](./leetcode/863.all-nodes-distance-k-in-binary-tree.cpp), [python3](./leetcode/863.all-nodes-distance-k-in-binary-tree.py) | Hash Table | O\(N\) | O\(N\) | - |
421422
| [Leetcode-432](https://leetcode.com/problems/all-oone-data-structure/) | All Oone Data Structure | [c++](./leetcode/432.all-oone-data-structure.cpp), [python3](./leetcode/432.all-oone-data-structure.py) | Hash Table | O\(1\) | O\(N\) | - |
422423
| [Leetcode-106](https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/) | Construct Binary Tree From Inorder And Postorder Traversal | [c++](./leetcode/106.construct-binary-tree-from-inorder-and-postorder-traversal.cpp), [python3](./leetcode/106.construct-binary-tree-from-inorder-and-postorder-traversal.py) | Hash Table | O\(N\) | O\(N\) | - |
423424
| [Leetcode-105](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) | Construct Binary Tree From Preorder And Inorder Traversal | [c++](./leetcode/105.construct-binary-tree-from-preorder-and-inorder-traversal.cpp), [python3](./leetcode/105.construct-binary-tree-from-preorder-and-inorder-traversal.py) | Hash Table | O\(N\) | O\(N\) | - |
@@ -704,10 +705,11 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
704705

705706
## Breadth-First Search
706707

707-
| Link | Problem(36) | Solution | Tag | Time | Space | Ref |
708+
| Link | Problem(37) | Solution | Tag | Time | Space | Ref |
708709
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
709710
| [Leetcode-542](https://leetcode.com/problems/01-matrix/) | 01 Matrix | [c++](./leetcode/542.01-matrix.cpp), [python3](./leetcode/542.01-matrix.py) | Breadth-First Search | O\(MN\) | O\(MN\) | - |
710711
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Breadth-First Search | O\(MlogM\) | O\(N\) | - |
712+
| [Leetcode-863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | All Nodes Distance K In Binary Tree | [c++](./leetcode/863.all-nodes-distance-k-in-binary-tree.cpp), [python3](./leetcode/863.all-nodes-distance-k-in-binary-tree.py) | Breadth-First Search | O\(N\) | O\(N\) | - |
711713
| [Leetcode-637](https://leetcode.com/problems/average-of-levels-in-binary-tree/) | Average Of Levels In Binary Tree | [c++](./leetcode/637.average-of-levels-in-binary-tree.cpp), [python3](./leetcode/637.average-of-levels-in-binary-tree.py) | Breadth-First Search | O\(N\) | O\(N\) | - |
712714
| [Leetcode-102](https://leetcode.com/problems/binary-tree-level-order-traversal/) | Binary Tree Level Order Traversal | [c++](./leetcode/102.binary-tree-level-order-traversal.cpp), [python3](./leetcode/102.binary-tree-level-order-traversal.py) | Breadth-First Search | O\(N\) | O\(Width\) | - |
713715
| [Leetcode-199](https://leetcode.com/problems/binary-tree-right-side-view/) | Binary Tree Right Side View | [c++](./leetcode/199.binary-tree-right-side-view.cpp), [python3](./leetcode/199.binary-tree-right-side-view.py) | Breadth-First Search | O\(N\) | O\(W\) | - |
@@ -745,9 +747,10 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
745747

746748
## Depth-First Search
747749

748-
| Link | Problem(51) | Solution | Tag | Time | Space | Ref |
750+
| Link | Problem(52) | Solution | Tag | Time | Space | Ref |
749751
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
750752
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Depth-First Search | O\(MlogM\) | O\(N\) | - |
753+
| [Leetcode-863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | All Nodes Distance K In Binary Tree | [c++](./leetcode/863.all-nodes-distance-k-in-binary-tree.cpp), [python3](./leetcode/863.all-nodes-distance-k-in-binary-tree.py) | Depth-First Search | O\(N\) | O\(N\) | - |
751754
| [Leetcode-637](https://leetcode.com/problems/average-of-levels-in-binary-tree/) | Average Of Levels In Binary Tree | [c++](./leetcode/637.average-of-levels-in-binary-tree.cpp), [python3](./leetcode/637.average-of-levels-in-binary-tree.py) | Depth-First Search | O\(N\) | O\(N\) | - |
752755
| [Leetcode-110](https://leetcode.com/problems/balanced-binary-tree/) | Balanced Binary Tree | [c++](./leetcode/110.balanced-binary-tree.cpp), [python3](./leetcode/110.balanced-binary-tree.py) | Depth-First Search | O\(N\) | O\(H\) | - |
753756
| [Leetcode-94](https://leetcode.com/problems/binary-tree-inorder-traversal/) | Binary Tree Inorder Traversal | [c++](./leetcode/94.binary-tree-inorder-traversal.cpp), [python3](./leetcode/94.binary-tree-inorder-traversal.py) | Depth-First Search | O\(N\) | O\(H\) | - |
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Tag: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// 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.
8+
// You can return the answer in any order.
9+
//  
10+
// Example 1:
11+
//
12+
//
13+
// Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
14+
// Output: [7,4,1]
15+
// Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
16+
//
17+
// Example 2:
18+
//
19+
// Input: root = [1], target = 1, k = 3
20+
// Output: []
21+
//
22+
//  
23+
// Constraints:
24+
//
25+
// The number of nodes in the tree is in the range [1, 500].
26+
// 0 <= Node.val <= 500
27+
// All the values Node.val are unique.
28+
// target is the value of one of the nodes in the tree.
29+
// 0 <= k <= 1000
30+
//
31+
//
32+
33+
/**
34+
* Definition for a binary tree node.
35+
* struct TreeNode {
36+
* int val;
37+
* TreeNode *left;
38+
* TreeNode *right;
39+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
40+
* };
41+
*/
42+
class Solution {
43+
public:
44+
vector<int> distanceK(TreeNode* root, TreeNode* target, int k) {
45+
unordered_map<int, vector<int>> graph;
46+
queue<TreeNode*> q;
47+
q.push(root);
48+
49+
while (!q.empty()) {
50+
TreeNode *cur = q.front();
51+
q.pop();
52+
53+
if (cur->left) {
54+
graph[cur->val].push_back(cur->left->val);
55+
graph[cur->left->val].push_back(cur->val);
56+
q.push(cur->left);
57+
}
58+
59+
if (cur->right) {
60+
graph[cur->val].push_back(cur->right->val);
61+
graph[cur->right->val].push_back(cur->val);
62+
q.push(cur->right);
63+
}
64+
}
65+
66+
return search(graph, target->val, k);
67+
}
68+
69+
vector<int> search(unordered_map<int, vector<int>> &graph, int target, int k) {
70+
vector<int> res;
71+
unordered_set<int> visited;
72+
queue<int> q;
73+
q.push(target);
74+
visited.insert(target);
75+
int level = 0;
76+
while (!q.empty()) {
77+
int size = q.size();
78+
for (int i = 0; i < size; i++) {
79+
int cur = q.front();
80+
q.pop();
81+
if (level == k) {
82+
res.push_back(cur);
83+
} else {
84+
for (int child: graph[cur]) {
85+
if (visited.count(child) == 0) {
86+
visited.insert(child);
87+
q.push(child);
88+
}
89+
}
90+
}
91+
}
92+
level += 1;
93+
}
94+
95+
return res;
96+
}
97+
};
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Tag: Hash Table, Tree, Depth-First Search, Breadth-First Search, Binary Tree
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# 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.
8+
# You can return the answer in any order.
9+
#  
10+
# Example 1:
11+
#
12+
#
13+
# Input: root = [3,5,1,6,2,0,8,null,null,7,4], target = 5, k = 2
14+
# Output: [7,4,1]
15+
# Explanation: The nodes that are a distance 2 from the target node (with value 5) have values 7, 4, and 1.
16+
#
17+
# Example 2:
18+
#
19+
# Input: root = [1], target = 1, k = 3
20+
# Output: []
21+
#
22+
#  
23+
# Constraints:
24+
#
25+
# The number of nodes in the tree is in the range [1, 500].
26+
# 0 <= Node.val <= 500
27+
# All the values Node.val are unique.
28+
# target is the value of one of the nodes in the tree.
29+
# 0 <= k <= 1000
30+
#
31+
#
32+
33+
# Definition for a binary tree node.
34+
# class TreeNode:
35+
# def __init__(self, x):
36+
# self.val = x
37+
# self.left = None
38+
# self.right = None
39+
from collections import deque, defaultdict
40+
class Solution:
41+
def distanceK(self, root: TreeNode, target: TreeNode, k: int) -> List[int]:
42+
graph = defaultdict(list)
43+
q = deque([root])
44+
while len(q) > 0:
45+
cur = q.popleft()
46+
if cur.left:
47+
graph[cur.val].append(cur.left.val)
48+
graph[cur.left.val].append(cur.val)
49+
q.append(cur.left)
50+
51+
if cur.right:
52+
graph[cur.val].append(cur.right.val)
53+
graph[cur.right.val].append(cur.val)
54+
q.append(cur.right)
55+
56+
res = []
57+
visited = set([target.val])
58+
q = deque([target.val])
59+
level = 0
60+
while len(q) > 0:
61+
size = len(q)
62+
for _ in range(size):
63+
cur = q.popleft()
64+
if level == k:
65+
res.append(cur)
66+
67+
for child in graph[cur]:
68+
if child not in visited:
69+
visited.add(child)
70+
q.append(child)
71+
72+
level += 1
73+
return res

lintcode/3662.design-hit-counter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Tag: Simulation
22
// Time: O(N)
33
// Space: O(N)
4-
// Ref: -
4+
// Ref: Leetcode-362
55
// Note: -
66

77
// In this question, you need to design a HitCounter class.

lintcode/3662.design-hit-counter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Tag: Simulation
22
# Time: O(N)
33
# Space: O(N)
4-
# Ref: -
4+
# Ref: Leetcode-362
55
# Note: -
66

77
# In this question, you need to design a HitCounter class.

list/grind169.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
121. https://leetcode.com/problems/maximal-square/
126126
122. https://leetcode.com/problems/rotate-image/
127127
123. https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/
128-
124. https://leetcode.com/problems/design-hit-counter/
128+
124. https://www.lintcode.com/problem/design-hit-counter/
129129
125. https://leetcode.com/problems/path-sum-iii/
130130
126. https://leetcode.com/problems/powx-n/
131131
127. https://leetcode.com/problems/search-a-2d-matrix/

list/leetcode-google.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@
383383
375. https://leetcode.com/problems/deepest-leaves-sum/
384384
376. https://leetcode.com/problems/pacific-atlantic-water-flow/
385385
377. https://leetcode.com/problems/remove-comments/
386-
378. https://leetcode.com/problems/design-hit-counter/
386+
378. https://www.lintcode.com/problem/design-hit-counter/
387387
379. https://leetcode.com/problems/find-the-difference-of-two-arrays/
388388
380. https://leetcode.com/problems/minimum-time-to-complete-trips/
389389
381. https://leetcode.com/problems/add-two-integers/

0 commit comments

Comments
 (0)