jiefancis / blogs

个人博客,学习笔记(issues)
1 stars 0 forks source link

算法系列之bfs和dfs:实现判断数组最大深度 #10

Open jiefancis opened 3 years ago

jiefancis commented 3 years ago

dfs实现

var tests = [1,[2,[3,[4],[5,[6,[7]],[6],[8]],[4],3]],0] maxDepth(tests)


## bfs实现

function maxDepth(nums) { let deep = 0; let temp = [...nums];

while(temp.length) {
  let length = temp.length;
  deep++
  while(length) {
      let item = temp.shift();
      if(Array.isArray(item)) {
          temp.push(...item)
      }
      length--
  }
}

return deep

}

var tests = [1,[2,[3,[4],[5,[6,[7]],[6],[8]],[4],3]],0] maxDepth(tests)