본문 바로가기
BEB/algorithm

Toy07 - tree dfs

by ddanss 2022. 11. 2.
728x90

      1

   2    3

4   5

이런식이니 1 -> 2 -> 4 -> 5 ->3

 

//레퍼런스 코드.
//이건 처음보는 느낌의 코드인데
//arr값을 찍어봤더니
//1
//2
//2,4
//2,4,5
//1,2,4,5
//1,2,4,5,3
//이런식으로 arr에 입력된다

let dfs = function (node) {
  let arr = [node.value];
  if(node.children.length>=1) {
    for(let i=0;i<node.children.length;i++) {
      arr = arr.concat(dfs(node.children[i]))
    }
  }
  return arr;
};

 

 

반응형

'BEB > algorithm' 카테고리의 다른 글

Toy8 - largestProductOfThree  (0) 2022.11.04
백트래킹  (0) 2022.11.03
Toy06 - Sudoku  (0) 2022.11.02
타일링 tiling  (0) 2022.10.31
[js] 버블정렬  (0) 2022.10.28

댓글