Skip to content

Commit df4dd69

Browse files
committed
feat: add solutions to lc problem: No.1261
No.1261.Find Elements in a Contaminated Binary Tree
1 parent 699bd43 commit df4dd69

File tree

6 files changed

+119
-167
lines changed

6 files changed

+119
-167
lines changed

solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README.md

+43-55
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ findElements.find(5); // return True
8787

8888
<!-- 这里可写通用的实现逻辑 -->
8989

90+
**方法一:DFS + 哈希表**
91+
92+
我们先通过 DFS 遍历二叉树,将节点值恢复为原来的值,然后再通过哈希表存储所有节点值,这样在查找时就可以直接判断目标值是否存在于哈希表中。
93+
94+
时间复杂度方面,初始化时需要遍历二叉树,时间复杂度为 $O(n)$,查找时只需要判断哈希表中是否存在目标值,时间复杂度为 $O(1)$。空间复杂度 $O(n)$。其中 $n$ 为二叉树节点个数。
95+
9096
<!-- tabs:start -->
9197

9298
### **Python3**
@@ -101,26 +107,23 @@ findElements.find(5); // return True
101107
# self.left = left
102108
# self.right = right
103109
class FindElements:
104-
def __init__(self, root: TreeNode):
105-
root.val = 0
106-
self.nodes = {0}
107110

111+
def __init__(self, root: Optional[TreeNode]):
108112
def dfs(root):
109-
if root is None:
110-
return
113+
self.vis.add(root.val)
111114
if root.left:
112115
root.left.val = root.val * 2 + 1
113-
self.nodes.add(root.left.val)
116+
dfs(root.left)
114117
if root.right:
115118
root.right.val = root.val * 2 + 2
116-
self.nodes.add(root.right.val)
117-
dfs(root.left)
118-
dfs(root.right)
119+
dfs(root.right)
119120

121+
root.val = 0
122+
self.vis = set()
120123
dfs(root)
121124

122125
def find(self, target: int) -> bool:
123-
return target in self.nodes
126+
return target in self.vis
124127

125128

126129
# Your FindElements object will be instantiated and called as such:
@@ -149,33 +152,27 @@ class FindElements:
149152
* }
150153
*/
151154
class FindElements {
152-
private Set<Integer> nodes;
155+
private Set<Integer> vis = new HashSet<>();
153156

154157
public FindElements(TreeNode root) {
155-
nodes = new HashSet<>();
156158
root.val = 0;
157-
nodes.add(0);
158159
dfs(root);
159160
}
160161

161-
public boolean find(int target) {
162-
return nodes.contains(target);
163-
}
164-
165162
private void dfs(TreeNode root) {
166-
if (root == null) {
167-
return;
168-
}
163+
vis.add(root.val);
169164
if (root.left != null) {
170165
root.left.val = root.val * 2 + 1;
171-
nodes.add(root.left.val);
166+
dfs(root.left);
172167
}
173168
if (root.right != null) {
174169
root.right.val = root.val * 2 + 2;
175-
nodes.add(root.right.val);
170+
dfs(root.right);
176171
}
177-
dfs(root.left);
178-
dfs(root.right);
172+
}
173+
174+
public boolean find(int target) {
175+
return vis.contains(target);
179176
}
180177
}
181178

@@ -202,32 +199,28 @@ class FindElements {
202199
*/
203200
class FindElements {
204201
public:
205-
unordered_set<int> nodes;
206-
207202
FindElements(TreeNode* root) {
208203
root->val = 0;
209-
nodes.clear();
210-
nodes.insert(0);
204+
function<void(TreeNode*)> dfs = [&](TreeNode* root) {
205+
vis.insert(root->val);
206+
if (root->left) {
207+
root->left->val = root->val * 2 + 1;
208+
dfs(root->left);
209+
}
210+
if (root->right) {
211+
root->right->val = root->val * 2 + 2;
212+
dfs(root->right);
213+
}
214+
};
211215
dfs(root);
212216
}
213217

214218
bool find(int target) {
215-
return nodes.count(target);
219+
return vis.count(target);
216220
}
217221

218-
void dfs(TreeNode* root) {
219-
if (!root) return;
220-
if (root->left) {
221-
root->left->val = root->val * 2 + 1;
222-
nodes.insert(root->left->val);
223-
}
224-
if (root->right) {
225-
root->right->val = root->val * 2 + 2;
226-
nodes.insert(root->right->val);
227-
}
228-
dfs(root->left);
229-
dfs(root->right);
230-
}
222+
private:
223+
unordered_set<int> vis;
231224
};
232225

233226
/**
@@ -249,35 +242,30 @@ public:
249242
* }
250243
*/
251244
type FindElements struct {
252-
nodes map[int]bool
245+
vis map[int]bool
253246
}
254247

255248
func Constructor(root *TreeNode) FindElements {
256249
root.Val = 0
257-
nodes := make(map[int]bool)
258-
nodes[0] = true
259-
var dfs func(root *TreeNode)
250+
vis := map[int]bool{}
251+
var dfs func(*TreeNode)
260252
dfs = func(root *TreeNode) {
261-
if root == nil {
262-
return
263-
}
253+
vis[root.Val] = true
264254
if root.Left != nil {
265255
root.Left.Val = root.Val*2 + 1
266-
nodes[root.Left.Val] = true
256+
dfs(root.Left)
267257
}
268258
if root.Right != nil {
269259
root.Right.Val = root.Val*2 + 2
270-
nodes[root.Right.Val] = true
260+
dfs(root.Right)
271261
}
272-
dfs(root.Left)
273-
dfs(root.Right)
274262
}
275263
dfs(root)
276-
return FindElements{nodes}
264+
return FindElements{vis}
277265
}
278266

279267
func (this *FindElements) Find(target int) bool {
280-
return this.nodes[target]
268+
return this.vis[target]
281269
}
282270

283271
/**

solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/README_EN.md

+38-56
Original file line numberDiff line numberDiff line change
@@ -90,26 +90,23 @@ findElements.find(5); // return True
9090
# self.left = left
9191
# self.right = right
9292
class FindElements:
93-
def __init__(self, root: TreeNode):
94-
root.val = 0
95-
self.nodes = {0}
9693

94+
def __init__(self, root: Optional[TreeNode]):
9795
def dfs(root):
98-
if root is None:
99-
return
96+
self.vis.add(root.val)
10097
if root.left:
10198
root.left.val = root.val * 2 + 1
102-
self.nodes.add(root.left.val)
99+
dfs(root.left)
103100
if root.right:
104101
root.right.val = root.val * 2 + 2
105-
self.nodes.add(root.right.val)
106-
dfs(root.left)
107-
dfs(root.right)
102+
dfs(root.right)
108103

104+
root.val = 0
105+
self.vis = set()
109106
dfs(root)
110107

111108
def find(self, target: int) -> bool:
112-
return target in self.nodes
109+
return target in self.vis
113110

114111

115112
# Your FindElements object will be instantiated and called as such:
@@ -136,33 +133,27 @@ class FindElements:
136133
* }
137134
*/
138135
class FindElements {
139-
private Set<Integer> nodes;
136+
private Set<Integer> vis = new HashSet<>();
140137

141138
public FindElements(TreeNode root) {
142-
nodes = new HashSet<>();
143139
root.val = 0;
144-
nodes.add(0);
145140
dfs(root);
146141
}
147142

148-
public boolean find(int target) {
149-
return nodes.contains(target);
150-
}
151-
152143
private void dfs(TreeNode root) {
153-
if (root == null) {
154-
return;
155-
}
144+
vis.add(root.val);
156145
if (root.left != null) {
157146
root.left.val = root.val * 2 + 1;
158-
nodes.add(root.left.val);
147+
dfs(root.left);
159148
}
160149
if (root.right != null) {
161150
root.right.val = root.val * 2 + 2;
162-
nodes.add(root.right.val);
151+
dfs(root.right);
163152
}
164-
dfs(root.left);
165-
dfs(root.right);
153+
}
154+
155+
public boolean find(int target) {
156+
return vis.contains(target);
166157
}
167158
}
168159

@@ -189,32 +180,28 @@ class FindElements {
189180
*/
190181
class FindElements {
191182
public:
192-
unordered_set<int> nodes;
193-
194183
FindElements(TreeNode* root) {
195184
root->val = 0;
196-
nodes.clear();
197-
nodes.insert(0);
185+
function<void(TreeNode*)> dfs = [&](TreeNode* root) {
186+
vis.insert(root->val);
187+
if (root->left) {
188+
root->left->val = root->val * 2 + 1;
189+
dfs(root->left);
190+
}
191+
if (root->right) {
192+
root->right->val = root->val * 2 + 2;
193+
dfs(root->right);
194+
}
195+
};
198196
dfs(root);
199197
}
200-
198+
201199
bool find(int target) {
202-
return nodes.count(target);
200+
return vis.count(target);
203201
}
204202

205-
void dfs(TreeNode* root) {
206-
if (!root) return;
207-
if (root->left) {
208-
root->left->val = root->val * 2 + 1;
209-
nodes.insert(root->left->val);
210-
}
211-
if (root->right) {
212-
root->right->val = root->val * 2 + 2;
213-
nodes.insert(root->right->val);
214-
}
215-
dfs(root->left);
216-
dfs(root->right);
217-
}
203+
private:
204+
unordered_set<int> vis;
218205
};
219206

220207
/**
@@ -236,35 +223,30 @@ public:
236223
* }
237224
*/
238225
type FindElements struct {
239-
nodes map[int]bool
226+
vis map[int]bool
240227
}
241228
242229
func Constructor(root *TreeNode) FindElements {
243230
root.Val = 0
244-
nodes := make(map[int]bool)
245-
nodes[0] = true
246-
var dfs func(root *TreeNode)
231+
vis := map[int]bool{}
232+
var dfs func(*TreeNode)
247233
dfs = func(root *TreeNode) {
248-
if root == nil {
249-
return
250-
}
234+
vis[root.Val] = true
251235
if root.Left != nil {
252236
root.Left.Val = root.Val*2 + 1
253-
nodes[root.Left.Val] = true
237+
dfs(root.Left)
254238
}
255239
if root.Right != nil {
256240
root.Right.Val = root.Val*2 + 2
257-
nodes[root.Right.Val] = true
241+
dfs(root.Right)
258242
}
259-
dfs(root.Left)
260-
dfs(root.Right)
261243
}
262244
dfs(root)
263-
return FindElements{nodes}
245+
return FindElements{vis}
264246
}
265247
266248
func (this *FindElements) Find(target int) bool {
267-
return this.nodes[target]
249+
return this.vis[target]
268250
}
269251
270252
/**

solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp

+15-19
Original file line numberDiff line numberDiff line change
@@ -11,32 +11,28 @@
1111
*/
1212
class FindElements {
1313
public:
14-
unordered_set<int> nodes;
15-
1614
FindElements(TreeNode* root) {
1715
root->val = 0;
18-
nodes.clear();
19-
nodes.insert(0);
16+
function<void(TreeNode*)> dfs = [&](TreeNode* root) {
17+
vis.insert(root->val);
18+
if (root->left) {
19+
root->left->val = root->val * 2 + 1;
20+
dfs(root->left);
21+
}
22+
if (root->right) {
23+
root->right->val = root->val * 2 + 2;
24+
dfs(root->right);
25+
}
26+
};
2027
dfs(root);
2128
}
22-
29+
2330
bool find(int target) {
24-
return nodes.count(target);
31+
return vis.count(target);
2532
}
2633

27-
void dfs(TreeNode* root) {
28-
if (!root) return;
29-
if (root->left) {
30-
root->left->val = root->val * 2 + 1;
31-
nodes.insert(root->left->val);
32-
}
33-
if (root->right) {
34-
root->right->val = root->val * 2 + 2;
35-
nodes.insert(root->right->val);
36-
}
37-
dfs(root->left);
38-
dfs(root->right);
39-
}
34+
private:
35+
unordered_set<int> vis;
4036
};
4137

4238
/**

0 commit comments

Comments
 (0)