这是输入代码:
var newTree = {};
newTree.value = value;
newTree.children = [] // fix me
Object.assign(newTree, treeMethods)
return newTree;
};
var treeMethods = {};
treeMethods.addChild = function(value) {
const newNode = Tree(value);
this.children.push(newNode)
};
treeMethods.contains = function(target) {
let currentTree = this.children
while (currentTree != null) {
if (currentTree.indexOf(target) > -1) return true
currentTree = this.children[target]
}
return false
};
我试图确定,通过 contains 函数给定目标值,我是否可以在父节点或后代节点中找到调用 contains() 的目标值。
请不要给我完整的答案,但如果你能把我推向正确的方向。