티스토리 뷰
https://www.acmicpc.net/problem/2250
2250번: 트리의 높이와 너비
첫째 줄에 노드의 개수를 나타내는 정수 N(1 ≤ N ≤ 10,000)이 주어진다. 다음 N개의 줄에는 각 줄마다 노드 번호와 해당 노드의 왼쪽 자식 노드와 오른쪽 자식 노드의 번호가 순서대로 주어진다.
www.acmicpc.net
class Node {
left = null;
right = null;
level = null;
location = null;
parent = null;
constructor(value) {
this.value = value;
}
setLevel(level) {
this.level = level;
}
setLeft(left) {
this.left = left;
}
setRight(right) {
this.right = right;
}
setParent(parent) {
this.parent = parent;
}
setLocation(loc) {
this.location = loc;
}
}
class Tree {
pointer = new Array(N).fill(null);
parents = new Array(N).fill(null);
location = 1;
root = null;
level = 0;
result;
constructor(nodes) {
this.nodes = nodes;
}
createNode() {
nodes.forEach((node) => {
const [v, l, r] = node;
const newNode = new Node(v);
this.pointer[v] = newNode;
if (l != -2) this.parents[l] = v;
if (r != -2) this.parents[r] = v;
});
}
findRoot() {
for (let i = 0; i < N; i++) {
if (this.parents[i] == null) {
this.root = this.pointer[i];
break;
}
}
}
draw() {
const q = [];
q.push([this.root, null, 0]);
while (q.length > 0) {
const [node, parent, level] = q.shift();
node.setParent(parent);
node.setLevel(level);
this.level = Math.max(level, this.level);
const [v, l, r] = this.nodes[node.value];
if (l != -2) {
node.setLeft(this.pointer[l]);
q.push([this.pointer[l], node, level + 1]);
}
if (r != -2) {
node.setRight(this.pointer[r]);
q.push([this.pointer[r], node, level + 1]);
}
}
this.result = Array.from(Array(this.level + 1), () => [Infinity, 0]);
}
findLeftMost(node) {
let leftMost = node;
while (leftMost.left != null) {
leftMost = leftMost.left;
}
return leftMost;
}
// findLocation() {
// const q = [];
// const leftMost = this.findLeftMost(this.root);
// leftMost.setLocation(this.location++);
// q.push(leftMost);
// console.log(this.parents);
// while (this.location <= N) {
// let now = q.shift();
// console.log(now.value);
// if (now.location != null) {
// if (now.parent != null) q.push(now.parent);
// } else if (now.right == null) {
// now.parent.setLocation(this.location++);
// q.push(now.parent);
// } else {
// const newLeft = this.findLeftMost(now.right);
// newLeft.setLocation(this.location++);
// q.push(newLeft);
// }
// }
// }
inorder(node) {
if (node) {
this.inorder(node.left);
node.location = this.location++;
this.inorder(node.right);
}
}
findResult(node) {
// console.log(node.value + 1, node.level + 1, node.location);
const [min, max] = this.result[node.level];
this.result[node.level] = [
Math.min(node.location, min),
Math.max(node.location, max),
];
if (node.left != null) {
this.findResult(node.left);
}
if (node.right != null) {
this.findResult(node.right);
}
}
printAnswer() {
let answer = [0, 0];
this.result.forEach((v, i) => {
const level = i + 1;
const [min, max] = v;
const length = max - min + 1;
if (answer[1] < length) {
answer[0] = level;
answer[1] = length;
}
});
console.log(answer.join(" "));
}
}
const fs = require("fs");
const input = fs
.readFileSync("./dev/stdin")
.toString()
.trim()
.split("\n")
.map((v) => v.split(" ").map(Number));
const [N] = input.shift();
const nodes = input.map((v) => v.map((x) => x - 1)).sort((a, b) => a[0] - b[0]);
const t = new Tree(nodes);
t.createNode();
t.findRoot();
t.draw();
t.inorder(t.root);
t.findResult(t.root);
t.printAnswer();
728x90
'자료구조 알고리즘 > 백준' 카테고리의 다른 글
Node.js)백준 20057번: 토네이도 (0) | 2022.08.15 |
---|---|
Node.js) 백준 3425번: 고스택 (0) | 2022.08.11 |
Node.js) 백준 1959번: 달팽이 3 (0) | 2022.08.08 |
Node.js)백준 1600번: 말이 되고픈 원숭이 (0) | 2022.08.05 |
Node.js) 백준 16933번: 벽 부수고 이동하기 3 (0) | 2022.08.04 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 투포인터 연습
- node.js
- 투포인터
- 개발자면접
- 서버점검
- MySQL
- MOD
- 면접비
- 최소공통조상
- DB 생성
- 다이나믹프로그래밍
- create db
- 면접질문
- 은둔청년체험
- 롱베케이션
- 로드나인
- KMP
- create databases;
- 다이나밍프로그래밍
- 서버개발
- BFS
- 동적프로그래밍
- 그래프
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
글 보관함