Skip to content

[환미니니] Week11 Solutions #552

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

Merged
merged 1 commit into from
Oct 27, 2024
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
45 changes: 45 additions & 0 deletions graph-valid-tree/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// V: Node 개수, E: 간선의 개수
// 시간복잡도: O(V + E)
// 공간복잡도: O(V + E)

class Solution {
/**
* @param {number} n
* @param {number[][]} edges
* @returns {boolean}
*/
validTree(n, edges) {
const graph = new Map();

for (let [u, v] of edges) {
if (!graph.has(u)) graph.set(u, [])
if (!graph.has(v)) graph.set(v, [])
graph.get(u).push(v)
graph.get(v).push(u)
}

const visited = Array.from({length: n}, () => false);

let edgeCount = 0;
const queue = [0];
visited[0] = true

while(queue.length) {
const cur = queue.shift();

for (const edge of graph.get(cur) || []) {
if (!visited[edge]) {
visited[edge] = true;
queue.push(edge)
}
edgeCount++;
}
}

const isAllVisited = visited.every((visited) => visited === true)

return isAllVisited && (edgeCount / 2) === (n - 1)
}


}
34 changes: 34 additions & 0 deletions maximum-depth-of-binary-tree/hwanmini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 시간복잡도: O(n)
// 공간복잡도: O(n)

/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if (!root) return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exploreMaxDepth에서 이미 !node일때에 대한 처리를 하고 있어서 없어도 괜찮을것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

엇 그렇네요
의견 감사합니다 : )


let maxDepth = 0;
const exploreMaxDepth = (node, depth) => {
if (!node) {
maxDepth = Math.max(maxDepth, depth)
return
}


exploreMaxDepth(node.left, depth + 1)
exploreMaxDepth(node.right, depth + 1)
}

exploreMaxDepth(root, 0)

return maxDepth
};