Skip to content

Commit

Permalink
update bst cheatsheet, add DataStructure/Node.java
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Sep 10, 2023
1 parent afa57a6 commit f745d3c
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
7 changes: 7 additions & 0 deletions doc/cheatsheet/bst.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ void traverse(TreeNode root) {
- the property of BST : inorder traversal of BST is an array sorted in the ascending order. (LC 230)
- Trim BST
- LC 669 (check [dfs.md](https://github.com/yennanliu/CS_basics/blob/master/doc/cheatsheet/dfs.md))
- BST's 2 pointers op
- LC 501
- [video](https://www.bilibili.com/video/BV1fD4y117gp/?share_source=copy_web)
- LC 530
- [video](https://www.bilibili.com/video/BV1DD4y11779/?share_source=copy_web)
- LC 538
- [video](https://www.bilibili.com/video/BV1d44y1f7wP/?share_source=copy_web)
- Algorithm
- dfs
Expand Down
24 changes: 24 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/DataStructure/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package LeetCodeJava.DataStructure;

import java.util.ArrayList;
import java.util.List;

// https://leetcode.com/problems/clone-graph/

public class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}

}
87 changes: 87 additions & 0 deletions leetcode_java/src/main/java/LeetCodeJava/Graph/CloneGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package LeetCodeJava.Graph;

import LeetCodeJava.DataStructure.Node;

import java.util.*;

// https://leetcode.com/problems/clone-graph/

/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/

public class CloneGraph {

// V0
Node clonedNode = new Node();
public Node cloneGraph(Node node) {

if (node == null){
return node;
}

// build map : {node_val : node_neighbors}
Map<Integer, Set<Node>> map = new HashMap<>();
for (Node x : node.neighbors){
int _val = x.val;
List<Node> _neighbors = x.neighbors;
if (!map.containsKey(_val)){
map.put(_val, new HashSet<>());
}
for (Node y : _neighbors){
map.get(_val).add(y);
}
}

List<Integer> visited = new ArrayList<>();
// (status) 0 : not visited, 1 : visiting, 2 : visited
int status = 0;
_help(node, visited, map, status);
return this.clonedNode;
}

private void _help(Node node, List<Integer> visited, Map<Integer, Set<Node>> map, int status){

// all nodes are visited
if (visited.size() == map.keySet().size()){
return;
}

if (!visited.contains(node)){
this.clonedNode = node;
visited.add(node.val);
if (map.get(node).isEmpty()){
status = 2;
map.remove(node.val);
}
}

for (Node _node : map.get(node)){
// remove visiting node in map val
map.get(_node.val).remove(_node);
_help(_node, visited, map, 1);
}

}

public static void main(String[] args) {
System.out.println(new Node());
}

}

0 comments on commit f745d3c

Please sign in to comment.