Skip to content
Merged
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
15 changes: 15 additions & 0 deletions Jiho/Day19/Leetcode_572. Subtree of Another Tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const isSubtree = (root, subRoot) => {
const areEqual = (node1, node2) => {
if (!node1 || !node2) return !node1 && !node2;
if (node1.val !== node2.val) return false;
return (
areEqual(node1.left, node2.left) && areEqual(node1.right, node2.right)
);
};
const dfs = (node) => {
if (!node) return false;
if (areEqual(node, subRoot)) return true;
return dfs(node.left) || dfs(node.right);
};
return dfs(root);
};
38 changes: 38 additions & 0 deletions Jiho/Day19/백준_2606_바이러스.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const fs = require("fs");
const input = fs
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "../input.txt")
.toString()
.trim()
.split("\n");

const [N, M, ...connectionsArr] = input;
const numOfDesktop = parseInt(N);
const numOfConnections = parseInt(M);
const connections = connectionsArr.map((elm) =>
elm.trim().split(" ").map(Number)
);
const graph = Array.from(Array(numOfDesktop + 1), () => new Array());

for (const [l, r] of connections) {
graph[l].push(r);
graph[r].push(l);
}

// Using Stack dfs
let answer = 0;

const stack = [1];
const visited = new Array(numOfDesktop + 1).fill(false);
visited[1] = true;
while (stack.length) {
const target = stack.pop();
answer += 1;
// push values connected to val while not visited
for (const val of graph[target]) {
if (!visited[val]) {
stack.push(val);
visited[val] = true;
}
}
}
console.log(answer - 1);