# 非递归 let deepTraversal2=(node)=>{ let stack =[]; let nodes=[]; if(node){ /*如果node节点存在*/ stack.push(node); while(stack.length){ let item= stack.pop();//取出队尾元素 let children = item.children;//获取元素子节点 nodes.push(children);//推进子节点,结构与父节点一样 if(children!==null||children.length!==0){ for(let i =0;i<children.length;i++){ stack.push(children[i]); } } } } return nodes; }
let widthTraversal2=(node)=>{ let nodes=[]; let stack=[]; if(node){ stack.push(node); while(stack.length){ let item = stack.shift(); let children = item.children; nodes.push(node); if(children!==null||children.length!==0){ for(let i =0;i<children.length;i++){ stack.push(children[i]); } } } } return nodes; }