
들어가기 전에, 개념 정리

그래서 오늘은,,,, DFS 중 Inorder Traversal를 탐구해보겠습니다!!
왼쪽 -> 중간 -> 오른쪽
Binary Tree일 때만 성립 가능
- root가 null이면, 다음을 반환..
- Inorder Traversal (루트 -> 왼쪽으로 이동)
- Process Root (루트 출력)
- Inorder Traversal (루트 -> 오른쪽으로 이동)
예시:

// JavaScript
// 이진 트리 구조
class Node {
constructor(v) {
this.data = v;
this.left = null;
this.right = null;
}
}
function printInorder(node) {
// 트리가 비어있거나 리프 노드의 자식인 경우,빈 값 반환
if (node === null) {
return;
}
// 노드의 왼쪽으로 이동
printInorder(node.left);
// 해당 노드값 출력
console.log(node.data);
// 노드의 오른쪽으로 이동
printInorder(node.right);
}
// 노드 입력
const root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(5);
root.right.right = new Node(6);
// Function call
printInorder(root); // 4 2 5 1 3 6
TIL:
+) 참고 자료:
https://www.geeksforgeeks.org/inorder-traversal-of-binary-tree/?ref=header_outind