当前位置: 代码迷 >> 综合 >> 深度遍历,广度遍历
  详细解决方案

深度遍历,广度遍历

热度:43   发布时间:2024-01-21 03:50:38.0

// 深度遍历

function interator(node) {

    console.log(node);

    if (node.children.length) {

        for (var i = 0; i < node.children.length; i++) {

            interator(node.children[i]);

        }

    }

}

 

// 广度遍历

function interator(node) {

 

    var arr = [];

    arr.push(node);

    while (arr.length > 0) {

        node = arr.shift();

        console.log(node);

        if (node.children.length) {

            for (var i = 0; i < node.children.length; i++) {

                arr.push(node.children[i]);

            }

        }

    }

}

  相关解决方案