728x90
반응형
References
아래 링크의 강의 중 Section 26. Tree Width with Level Width
의 내용을 추려 이번 글을 작성하였습니다.
The Coding Interview Bootcamp: Algorithms + Data Structures on Udemy
Tree Width
function levelWidth(root) {
const arr = [root, "s"];
const widths = [0];
while (arr.length > 1) {
const node = arr.shift();
if (node === "s") {
widths.push(0);
arr.push("s");
} else {
arr.push(...node.children);
widths[widths.length - 1]++;
}
}
return widths;
}
각 단계에 포함된 값들을 arr
에 넣어서 배열 arr
의 길이를 width
로서 저장하고, 중간에 stopper
역할을 하는 문자 s
를 넣어 탐색 작업 중 문자 s
와 만나면 다음 단계로 넘어가게끔 한다.
함께 보기
728x90
반응형
'👩💻 Programming > Algorithms & Data Structures' 카테고리의 다른 글
이진 탐색 트리 유효성 검사(Validating a Binary Search Tree) (0) | 2022.04.07 |
---|---|
이진 탐색 트리(Binary Search Trees) (0) | 2022.04.06 |
트리(Tree) (0) | 2022.04.05 |
순환 리스트(Circular List) (0) | 2022.04.05 |
연결 리스트(Linked Lists) (0) | 2022.04.05 |
댓글