-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsolution.js
56 lines (47 loc) · 1.02 KB
/
solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const getSet = (root) => {
root.val = 0
const set = new Set()
const nodeQueue = [root]
while (nodeQueue.length > 0) {
const node = nodeQueue.shift()
if (node.left) {
const value = (2 * node.val) + 1
node.left.val = value
nodeQueue.push(node.left)
set.add(value)
}
if (node.right) {
const value = (2 * node.val) + 2
node.right.val = value
nodeQueue.push(node.right)
set.add(value)
}
}
return set
}
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
*/
const FindElements = function (root) {
this.set = getSet(root)
}
/**
* @param {number} target
* @return {boolean}
*/
FindElements.prototype.find = function (target) {
return this.set.has(target)
}
/**
* Your FindElements object will be instantiated and called as such:
* var obj = new FindElements(root)
* var param_1 = obj.find(target)
*/
module.exports = FindElements