Skip to content

Commit

Permalink
Merge pull request #552 from JEONGHWANMIN/main
Browse files Browse the repository at this point in the history
[ํ™˜๋ฏธ๋‹ˆ๋‹ˆ] Week11 Solutions
  • Loading branch information
SamTheKorean authored Oct 27, 2024
2 parents 052d8b1 + f4721ee commit c4a9d7b
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
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;

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
};

0 comments on commit c4a9d7b

Please sign in to comment.