搜索
二分法
树结构查找
function findNodeById (tree, id) {
if (!tree.length) return null
const search = (node) => {
if (node.id === id) {
return node
} else if (node.children) {
for (const child of node.children) {
const result = search(child)
if (result) {
return result
}
}
}
return null
}
for (const root of tree) {
const result = search(root)
if (result) {
return result
}
}
return null
}