@@ -94,15 +94,200 @@ findElements.find(5); // return True
94
94
<!-- 这里可写当前语言的特殊实现逻辑 -->
95
95
96
96
``` python
97
-
97
+ # Definition for a binary tree node.
98
+ # class TreeNode:
99
+ # def __init__(self, val=0, left=None, right=None):
100
+ # self.val = val
101
+ # self.left = left
102
+ # self.right = right
103
+ class FindElements :
104
+
105
+ def __init__ (self , root : TreeNode):
106
+ root.val = 0
107
+ self .nodes = {0 }
108
+
109
+ def dfs (root ):
110
+ if root is None :
111
+ return
112
+ if root.left:
113
+ root.left.val = root.val * 2 + 1
114
+ self .nodes.add(root.left.val)
115
+ if root.right:
116
+ root.right.val = root.val * 2 + 2
117
+ self .nodes.add(root.right.val)
118
+ dfs(root.left)
119
+ dfs(root.right)
120
+
121
+ dfs(root)
122
+
123
+ def find (self , target : int ) -> bool :
124
+ return target in self .nodes
125
+
126
+
127
+ # Your FindElements object will be instantiated and called as such:
128
+ # obj = FindElements(root)
129
+ # param_1 = obj.find(target)
98
130
```
99
131
100
132
### ** Java**
101
133
102
134
<!-- 这里可写当前语言的特殊实现逻辑 -->
103
135
104
136
``` java
137
+ /**
138
+ * Definition for a binary tree node.
139
+ * public class TreeNode {
140
+ * int val;
141
+ * TreeNode left;
142
+ * TreeNode right;
143
+ * TreeNode() {}
144
+ * TreeNode(int val) { this.val = val; }
145
+ * TreeNode(int val, TreeNode left, TreeNode right) {
146
+ * this.val = val;
147
+ * this.left = left;
148
+ * this.right = right;
149
+ * }
150
+ * }
151
+ */
152
+ class FindElements {
153
+ private Set<Integer > nodes;
154
+
155
+ public FindElements (TreeNode root ) {
156
+ nodes = new HashSet<> ();
157
+ root. val = 0 ;
158
+ nodes. add(0 );
159
+ dfs(root);
160
+ }
161
+
162
+ public boolean find (int target ) {
163
+ return nodes. contains(target);
164
+ }
165
+
166
+ private void dfs (TreeNode root ) {
167
+ if (root == null ) {
168
+ return ;
169
+ }
170
+ if (root. left != null ) {
171
+ root. left. val = root. val * 2 + 1 ;
172
+ nodes. add(root. left. val);
173
+ }
174
+ if (root. right != null ) {
175
+ root. right. val = root. val * 2 + 2 ;
176
+ nodes. add(root. right. val);
177
+ }
178
+ dfs(root. left);
179
+ dfs(root. right);
180
+ }
181
+ }
182
+
183
+ /**
184
+ * Your FindElements object will be instantiated and called as such:
185
+ * FindElements obj = new FindElements(root);
186
+ * boolean param_1 = obj.find(target);
187
+ */
188
+ ```
189
+
190
+ ### ** C++**
191
+
192
+ ``` cpp
193
+ /* *
194
+ * Definition for a binary tree node.
195
+ * struct TreeNode {
196
+ * int val;
197
+ * TreeNode *left;
198
+ * TreeNode *right;
199
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
200
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
201
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
202
+ * };
203
+ */
204
+ class FindElements {
205
+ public:
206
+ unordered_set<int > nodes;
207
+
208
+ FindElements(TreeNode* root) {
209
+ root->val = 0;
210
+ nodes.clear();
211
+ nodes.insert(0);
212
+ dfs(root);
213
+ }
214
+
215
+ bool find (int target) {
216
+ return nodes.count(target);
217
+ }
218
+
219
+ void dfs(TreeNode* root) {
220
+ if (!root) return;
221
+ if (root->left)
222
+ {
223
+ root->left->val = root->val * 2 + 1;
224
+ nodes.insert(root->left->val);
225
+ }
226
+ if (root->right)
227
+ {
228
+ root->right->val = root->val * 2 + 2;
229
+ nodes.insert(root->right->val);
230
+ }
231
+ dfs(root->left);
232
+ dfs(root->right);
233
+ }
234
+ };
235
+
236
+ /**
237
+ * Your FindElements object will be instantiated and called as such:
238
+ * FindElements* obj = new FindElements(root);
239
+ * bool param_1 = obj->find(target);
240
+ * /
241
+ ```
105
242
243
+ ### **Go**
244
+
245
+ ```go
246
+ /**
247
+ * Definition for a binary tree node.
248
+ * type TreeNode struct {
249
+ * Val int
250
+ * Left *TreeNode
251
+ * Right *TreeNode
252
+ * }
253
+ */
254
+ type FindElements struct {
255
+ nodes map[int]bool
256
+ }
257
+
258
+ func Constructor(root *TreeNode) FindElements {
259
+ root.Val = 0
260
+ nodes := make(map[int]bool)
261
+ nodes[0] = true
262
+ var dfs func(root *TreeNode)
263
+ dfs = func(root *TreeNode) {
264
+ if root == nil {
265
+ return
266
+ }
267
+ if root.Left != nil {
268
+ root.Left.Val = root.Val*2 + 1
269
+ nodes[root.Left.Val] = true
270
+ }
271
+ if root.Right != nil {
272
+ root.Right.Val = root.Val*2 + 2
273
+ nodes[root.Right.Val] = true
274
+ }
275
+ dfs(root.Left)
276
+ dfs(root.Right)
277
+ }
278
+ dfs(root)
279
+ return FindElements{nodes}
280
+ }
281
+
282
+ func (this *FindElements) Find(target int) bool {
283
+ return this.nodes[target]
284
+ }
285
+
286
+ /**
287
+ * Your FindElements object will be instantiated and called as such:
288
+ * obj := Constructor(root);
289
+ * param_1 := obj.Find(target);
290
+ */
106
291
```
107
292
108
293
### ** ...**
0 commit comments