查找路径 function findItem(tree, target) { const res = []; function backtrack(tree, target, path) { if(!tree) return; const index = tree.findIndex(({ id }) => id === target); if(index >= 0) { path.push(tree[index].id); res.push(...path); return; } for(let i = 0; i < tree.length; i++) { path.push(tree[i].id); backtrack(tree[i].children, target, path); path.pop(); } } backtrack(tree, target, []); return res; }