-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dd3f08f
commit f4721ee
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |