// 二叉树的生成 function NodeTree(value) { this.value = value; this.left = null; this.right = null; } let ta = new NodeTree('a'); let tb = new NodeTree('b'); let tc = new NodeTree('c'); let td = new NodeTree('d'); let te = new NodeTree('e'); let tf = new NodeTree('f'); let tg = new NodeTree('g'); ta.left = tb; ta.right = tc; tb.left = td; tb.right = te; tc.left = tf; tc.right = tg; /** * 二叉树的深度优先搜索 * @param nodeTree * @param target * @returns {boolean|*} */ function deepSearch(nodeTree, target) { if (nodeTree === null) return false; if (nodeTree.value === target) return true; return deepSearch(nodeTree.left, target) || deepSearch(nodeTree.right, target); } // console.log(deepSearch(ta, 'e')); function spanSearch(nodeTreeList, target) { if (!nodeTreeList || nodeTreeList.length === 0) return false; let childNodeList = []; for (let i = 0, l = nodeTreeList.length; i < l; i++) { if ( nodeTreeList[i] !== null && nodeTreeList[i].value === target) { return true }else { if (nodeTreeList[i].left || nodeTreeList[i].right){ childNodeList.push(nodeTreeList[i].left) childNodeList.push(nodeTreeList[i].right) } } } return spanSearch(childNodeList, target) } console.log(spanSearch([ta], 'h'))