mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-01 18:55:23 +08:00
* feat(ko): add the complete Korean online edition * Use UTF-8 for Python CI output on Windows * Remove print-book promotion from the Korean site * fix(ko): preserve emphasis before Korean particles
61 lines
1.6 KiB
JavaScript
61 lines
1.6 KiB
JavaScript
/**
|
|
* File: binary_tree_dfs.js
|
|
* Created Time: 2022-12-04
|
|
* Author: IsChristina ([email protected])
|
|
*/
|
|
|
|
const { arrToTree } = require('../modules/TreeNode');
|
|
const { printTree } = require('../modules/PrintUtil');
|
|
|
|
// 순회 순서를 저장할 리스트를 초기화한다
|
|
const list = [];
|
|
|
|
/* 전위 순회 */
|
|
function preOrder(root) {
|
|
if (root === null) return;
|
|
// 방문 우선순위: 루트 노드 -> 왼쪽 서브트리 -> 오른쪽 서브트리
|
|
list.push(root.val);
|
|
preOrder(root.left);
|
|
preOrder(root.right);
|
|
}
|
|
|
|
/* 중위 순회 */
|
|
function inOrder(root) {
|
|
if (root === null) return;
|
|
// 방문 우선순위: 왼쪽 서브트리 -> 루트 노드 -> 오른쪽 서브트리
|
|
inOrder(root.left);
|
|
list.push(root.val);
|
|
inOrder(root.right);
|
|
}
|
|
|
|
/* 후위 순회 */
|
|
function postOrder(root) {
|
|
if (root === null) return;
|
|
// 방문 우선순위: 왼쪽 서브트리 -> 오른쪽 서브트리 -> 루트 노드
|
|
postOrder(root.left);
|
|
postOrder(root.right);
|
|
list.push(root.val);
|
|
}
|
|
|
|
/* 실행 코드 */
|
|
/* 이진 트리 초기화 */
|
|
// 여기서는 배열로부터 이진 트리를 바로 생성하는 함수를 사용한다
|
|
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
|
|
console.log('\n이진 트리 초기화\n');
|
|
printTree(root);
|
|
|
|
/* 전위 순회 */
|
|
list.length = 0;
|
|
preOrder(root);
|
|
console.log('\n전위 순회의 노드 출력 순서 = ' + list);
|
|
|
|
/* 중위 순회 */
|
|
list.length = 0;
|
|
inOrder(root);
|
|
console.log('\n중위 순회의 노드 출력 순서 = ' + list);
|
|
|
|
/* 후위 순회 */
|
|
list.length = 0;
|
|
postOrder(root);
|
|
console.log('\n후위 순회의 노드 출력 순서 = ' + list);
|