深度优先遍历的算法口诀
    访问根节点
    对根节点的children挨个进行深度优先遍历

    1. const dfs = (root) => {
    2. console.log(root.val)
    3. root.children.foreach(dfs(item))
    4. }

    shift()方法用于删除数组的第一个元素,并返回该元素。注意,该方法会改变原数组。

    广度优先遍历口诀
    新建一个队列,把根节点入队
    把队头出队并访问
    把对头的children挨个入队
    重复第二、三步操作,直到队列为空

    1. const bfs = (root) => {
    2. const q = [root]
    3. while(q.length > 0) {
    4. const n = q.shift()
    5. console.log(n.val)
    6. n.children.foreach(child => {
    7. q.push(child)
    8. })
    9. }
    10. }