Skip to content

[지원] 8-4. 부분집합 구하기 #330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
function solution(n) {
let answer = '';
let answer = {
preOrder: "",
postOrder: "",
};

const tree = {
1: [2, 3],
2: [4, 5],
3: [6, 7],
4: [null, null],
5: [null, null],
6: [null, null],
7: [null, null],
};

function preOrder(node) {
if (!node) return; // 기저조건
answer.preOrder += node + " "; // 현재 노드 번호 추가
preOrder(tree[node][0]); // 왼쪽 자식을 재귀 호출
preOrder(tree[node][1]); // 오른쪽 자식을 재귀 호출
}

function postOrder(node) {
if (!node) return;
postOrder(tree[node][0]);
postOrder(tree[node][1]);
answer.postOrder += node + " ";
}

preOrder(n);
postOrder(n);
return answer;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
function solution(n) {
let answer = [];
let subset = []; // 현재까지 선택된 부분 집합

// L: 현재 원소 번호
function DFS(L) {
if (L === n + 1) {
// 모든 원소를 탐색한 경우
answer.push([...subset]);
} else {
subset.push(L);
DFS(L + 1);

subset.pop();
DFS(L + 1);
}
}

DFS(1);
return answer;
}

Expand Down