Skip to content

1261-find-elements-in-a-contaminated-binary-tree

DevGod
DevGod
Vtuber
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
*/
var FindElements = function(root) {
this.tree = root;
};
/**
* @param {number} target
* @return {boolean}
*/
FindElements.prototype.find = function(target) {
let targetPath = (target+1).toString(2).slice(1);
let pointer = this.tree;
for(let path of targetPath){
if(pointer === null){return false;}
pointer = path==="0" ? pointer.left : pointer.right;
}
return pointer !== null;
};
/**
* Your FindElements object will be instantiated and called as such:
* var obj = new FindElements(root)
* var param_1 = obj.find(target)
*/