Skip to content

Commit 7b6e94f

Browse files
committed
🔥 Clone Graph
1 parent f9ee0f1 commit 7b6e94f

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ Solutions of LeetCode Blind 75 Problems in JavaScript
2323
| [Reverse Bits]() | | | [:link:](https://leetcode.com/problems/reverse-bits/) |
2424
| [Climbing Stairs](./climbing-stairs.js) | <img src="https://img.shields.io/badge/-Easy-green" /> | `Math`, `Dynamic Programming`, `Memoization` | [:link:](https://leetcode.com/problems/climbing-stairs/) |
2525
| [Word Search](./word-search.js) | <img src="https://img.shields.io/badge/-Medium-orange" /> | `Array`, `Backtracking`, `Matrix` | [:link:](https://leetcode.com/problems/word-search/) |
26+
| [Clone Graph](./clone-graph.js) | <img src="https://img.shields.io/badge/-Medium-orange" /> | `Graph`, `HashTable`, `DFS`, `BFS` | [:link:](https://leetcode.com/problems/clone-graph/) |

clone-graph.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const cloneGraph = (node) => {
2+
const map = {};
3+
4+
const traverse = (n) => {
5+
if (!n) return n;
6+
7+
if (!map[n.val]) {
8+
map[n.val] = new Node(n.val);
9+
map[n.val].neighbors = n.neighbors.map(traverse);
10+
}
11+
12+
return map[n.val];
13+
};
14+
15+
return traverse(node);
16+
};

0 commit comments

Comments
 (0)