[Java] Leetcode 133. Clone Graph [Search #7]

  Рет қаралды 3,670

Eric Programming

Eric Programming

Күн бұрын

Пікірлер: 6
@EricProgramming
@EricProgramming 3 жыл бұрын
class Solution { Map table = new HashMap(); public Node cloneGraph(Node node) { //Base case if(node == null) return null; //Define a queue Queue queue = new LinkedList(); //Define a visited set to keep track visited nodes Set visited = new HashSet(); //Add current node to the queue queue.add(node); //BFS while(!queue.isEmpty()){ //Take the first from queue Node first = queue.poll(); if(visited.contains(first)) continue; //Mark this node as visited visited.add(first); //If this node is not create in the table, create it Node newFirst = getNewNode(first); List neighbors = first.neighbors; //Iterate all the connect nodes from this node for(Node cur : neighbors){ //For each node, if not created, then create it in the table Node newCurNode = getNewNode(cur); newFirst.neighbors.add(newCurNode); //If this node is not visited, add to the queue if(!visited.contains(cur)){ queue.add(cur); } } } return table.get(node); } private Node getNewNode(Node node){ if(table.containsKey(node)) return table.get(node); int val = node.val; Node newNode = new Node(val); table.put(node, newNode); return newNode; } }
@gordonlichunfu
@gordonlichunfu 2 жыл бұрын
Clear, and to the point!
@annabellesun4719
@annabellesun4719 2 жыл бұрын
Thank you, Eric, your video and demo are very clear and helpful. :D!!
@EricProgramming
@EricProgramming 2 жыл бұрын
Glad it was helpful!
@raj_kundalia
@raj_kundalia Жыл бұрын
thank you!
@chaitanyasharma6270
@chaitanyasharma6270 2 жыл бұрын
your search playlist has one video hidden what question was that?
Clone Graph - Depth First Search - Leetcode 133
11:48
NeetCode
Рет қаралды 230 М.
[Java] Leetcode 130. Surrounded Regions [Search #3]
13:37
Eric Programming
Рет қаралды 2,2 М.
Кто круче, как думаешь?
00:44
МЯТНАЯ ФАНТА
Рет қаралды 5 МЛН
How Many Balloons To Make A Store Fly?
00:22
MrBeast
Рет қаралды 96 МЛН
World’s strongest WOMAN vs regular GIRLS
00:56
A4
Рет қаралды 51 МЛН
[Java] Topological Sort Algorithm | Solve Course Schedule I & II
33:58
Eric Programming
Рет қаралды 3,9 М.
Clone Graph| Leetcode 133 | Live coding session 🔥🔥 | BFS traversal
11:44
Clone Graph - Leetcode 133 - Graphs (Python)
13:38
Greg Hogg
Рет қаралды 3,8 М.
Clone graph | Leetcode #133
13:01
Techdose
Рет қаралды 59 М.
Clone Graph - 133. LeetCode - Java
6:50
Teddy Smith
Рет қаралды 278
5 Steps to Problem Solve Coding Interview Questions
14:10
Eric Programming
Рет қаралды 3,7 М.
[Java] Leetcode 417. Pacific Atlantic Water Flow [Search #4]
13:59
Eric Programming
Рет қаралды 3,2 М.
Learn Graphs in 5 minutes 🌐
5:17
Bro Code
Рет қаралды 133 М.
Breadth First Search (BFS): Visualized and Explained
10:41
Reducible
Рет қаралды 220 М.