Skip to content

Commit

Permalink
update 297 java, cheatsheet, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 12, 2024
1 parent 5cd622b commit d492a73
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 63 deletions.
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240312: 297(again)
20240311: 261(again!!!),268,269(again),271,295(again)
20240310: 213,217,226,230,235,238,242,252,253
20240309: 207(again!!!),208,211(again),212
Expand Down
16 changes: 9 additions & 7 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2024-05-06 -> ['297(again)']
2024-05-05 -> ['261(again!!!),268,269(again),271,295(again)']
2024-05-04 -> ['213,217,226,230,235,238,242,252,253']
2024-05-03 -> ['207(again!!!),208,211(again),212']
Expand All @@ -10,6 +11,7 @@
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-15 -> ['297(again)']
2024-04-14 -> ['261(again!!!),268,269(again),271,295(again)']
2024-04-13 -> ['213,217,226,230,235,238,242,252,253']
2024-04-12 -> ['207(again!!!),208,211(again),212']
Expand All @@ -20,27 +22,27 @@
2024-04-07 -> ['73,76,79(again),91,25']
2024-04-06 -> ['55(again),56,62,70']
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-02 -> ['20,21,23,33(again)']
2024-04-02 -> ['297(again)', '20,21,23,33(again)']
2024-04-01 -> ['261(again!!!),268,269(again),271,295(again)', '1,3,5,4,19']
2024-03-31 -> ['213,217,226,230,235,238,242,252,253']
2024-03-30 -> ['207(again!!!),208,211(again),212']
2024-03-29 -> ['152,153(again),190,191,198,200,206(again)']
2024-03-28 -> ['139(again),141,143(again)']
2024-03-27 -> ['121,124(again),125,128,133(again)']
2024-03-26 -> ['100,102,104,105(again)']
2024-03-25 -> ['73,76,79(again),91,25']
2024-03-25 -> ['297(again)', '73,76,79(again),91,25']
2024-03-24 -> ['261(again!!!),268,269(again),271,295(again)', '55(again),56,62,70']
2024-03-23 -> ['213,217,226,230,235,238,242,252,253']
2024-03-22 -> ['207(again!!!),208,211(again),212']
2024-03-21 -> ['152,153(again),190,191,198,200,206(again)', '39,48(again),49,53,54']
2024-03-20 -> ['139(again),141,143(again)', '20,21,23,33(again)']
2024-03-20 -> ['297(again)', '139(again),141,143(again)', '20,21,23,33(again)']
2024-03-19 -> ['261(again!!!),268,269(again),271,295(again)', '121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-18 -> ['213,217,226,230,235,238,242,252,253', '100,102,104,105(again)']
2024-03-17 -> ['207(again!!!),208,211(again),212', '73,76,79(again),91,25']
2024-03-17 -> ['297(again)', '207(again!!!),208,211(again),212', '73,76,79(again),91,25']
2024-03-16 -> ['261(again!!!),268,269(again),271,295(again)', '152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-15 -> ['213,217,226,230,235,238,242,252,253', '139(again),141,143(again)']
2024-03-14 -> ['261(again!!!),268,269(again),271,295(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-13 -> ['261(again!!!),268,269(again),271,295(again)', '213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-15 -> ['297(again)', '213,217,226,230,235,238,242,252,253', '139(again),141,143(again)']
2024-03-14 -> ['297(again)', '261(again!!!),268,269(again),271,295(again)', '207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-13 -> ['297(again)', '261(again!!!),268,269(again),271,295(again)', '213,217,226,230,235,238,242,252,253', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['261(again!!!),268,269(again),271,295(again)', '213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '139(again),141,143(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['213,217,226,230,235,238,242,252,253', '207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '121,124(again),125,128,133(again)', '55(again),56,62,70', '1,3,5,4,19']
2024-03-10 -> ['207(again!!!),208,211(again),212', '152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '100,102,104,105(again)']
Expand Down
58 changes: 58 additions & 0 deletions doc/cheatsheet/dfs.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,64 @@ class Codec:
return root
```

```java
// java
// LC 297
public class Codec{
public String serialize(TreeNode root) {

/** NOTE !!!
*
* if root == null, return "#"
*/
if (root == null){
return "#";
}

/** NOTE !!! return result via pre-order, split with "," */
return root.val + "," + serialize(root.left) + "," + serialize(root.right);
}

public TreeNode deserialize(String data) {

/** NOTE !!!
*
* 1) init queue and append serialize output
* 2) even use queue, but helper func still using DFS
*/
Queue<String> queue = new LinkedList<>(Arrays.asList(data.split(",")));
return helper(queue);
}

private TreeNode helper(Queue<String> queue) {

// get val from queue first
String s = queue.poll();

if (s.equals("#")){
return null;
}
/** NOTE !!! init current node */
TreeNode root = new TreeNode(Integer.valueOf(s));
/** NOTE !!!
*
* since serialize is "pre-order",
* deserialize we use "pre-order" as well
* e.g. root -> left sub tree -> right sub tree
* -> so we get sub tree via below :
*
* root.left = helper(queue);
* root.right = helper(queue);
*
*/
root.left = helper(queue);
root.right = helper(queue);
/** NOTE !!! don't forget to return final deserialize result */
return root;
}
}
```

#### 1-1-8) Serialize and Deserialize BST
```python
# LC 449. Serialize and Deserialize BST
Expand Down
61 changes: 61 additions & 0 deletions doc/cheatsheet/tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,67 @@ TreeNode deserlialize(String data){
}
```


#### 1-1-14-1) Serialize and Deserialize Binary Tree

```java
// java
// LC 297
public class Codec{
public String serialize(TreeNode root) {

/** NOTE !!!
*
* if root == null, return "#"
*/
if (root == null){
return "#";
}

/** NOTE !!! return result via pre-order, split with "," */
return root.val + "," + serialize(root.left) + "," + serialize(root.right);
}

public TreeNode deserialize(String data) {

/** NOTE !!!
*
* 1) init queue and append serialize output
* 2) even use queue, but helper func still using DFS
*/
Queue<String> queue = new LinkedList<>(Arrays.asList(data.split(",")));
return helper(queue);
}

private TreeNode helper(Queue<String> queue) {

// get val from queue first
String s = queue.poll();

if (s.equals("#")){
return null;
}
/** NOTE !!! init current node */
TreeNode root = new TreeNode(Integer.valueOf(s));
/** NOTE !!!
*
* since serialize is "pre-order",
* deserialize we use "pre-order" as well
* e.g. root -> left sub tree -> right sub tree
* -> so we get sub tree via below :
*
* root.left = helper(queue);
* root.right = helper(queue);
*
*/
root.left = helper(queue);
root.right = helper(queue);
/** NOTE !!! don't forget to return final deserialize result */
return root;
}
}
```

### 1-1-15) Invert Binary Tree
```python
# LC 226 Invert Binary Tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,60 +24,60 @@
public class SerializeAndDeserializeBinaryTree {

// VO
// TODO : fix it
// public class Codec {
// String res = "";
// // should use BFS (visit layer by layer)
// public String bfs(TreeNode root) {
// Queue<TreeNode> queue = new LinkedList<>();
// queue.add(root);
// while (!queue.isEmpty()) {
// TreeNode cur = queue.poll();
// if (cur == null){
// this.res += "#" + " ";
// continue;
// }
// System.out.println(">>> cur = " + cur.val);
// this.res += cur.val + " ";
// queue.add(cur.left);
// queue.add(cur.right);
// }
// return this.res;
// }
//
// // Encodes a tree to a single string.
// public String serialize(TreeNode root) {
// if (root == null) {
// return "";
// }
// if (root.left == null && root.right == null) {
// return String.valueOf(root.val);
// }
// String finalRes = bfs(root);
// System.out.println(">>> finalRes = " + finalRes);
// return finalRes;
// }
//
// // Decodes your encoded data to tree.
// public TreeNode deserialize(String data) {
// TreeNode deCodeRes = new TreeNode();
// Queue<TreeNode> queue = new LinkedList<>();
// if (data == null || data.length() == 0){
// return deCodeRes;
// }
// // use BFS again : add decoded string acquired via BFS
// String[] dataList = data.split(" ");
// for (String x: dataList){
// if (x == "#"){
// deCodeRes = null;
// }
// deCodeRes.val = Integer.parseInt(x);
// queue.add(deCodeRes);
// }
//
// return deCodeRes;
// }
// }
// IDEA : DFS
public class Codec{
public String serialize(TreeNode root) {

/** NOTE !!!
*
* if root == null, return "#"
*/
if (root == null){
return "#";
}

/** NOTE !!! return result via pre-order, split with "," */
return root.val + "," + serialize(root.left) + "," + serialize(root.right);
}

public TreeNode deserialize(String data) {

/** NOTE !!!
*
* 1) init queue and append serialize output
* 2) even use queue, but helper func still using DFS
*/
Queue<String> queue = new LinkedList<>(Arrays.asList(data.split(",")));
return helper(queue);
}

private TreeNode helper(Queue<String> queue) {

// get val from queue first
String s = queue.poll();

if (s.equals("#")){
return null;
}
/** NOTE !!! init current node */
TreeNode root = new TreeNode(Integer.valueOf(s));
/** NOTE !!!
*
* since serialize is "pre-order",
* deserialize we use "pre-order" as well
* e.g. root -> left sub tree -> right sub tree
* -> so we get sub tree via below :
*
* root.left = helper(queue);
* root.right = helper(queue);
*
*/
root.left = helper(queue);
root.right = helper(queue);
/** NOTE !!! don't forget to return final deserialize result */
return root;
}
}

// V1
// IDEA : BFS + QUEUE OP
Expand Down Expand Up @@ -123,13 +123,15 @@ public TreeNode deserialize(String data) {
for (int i = 1; i < len; i++) {
TreeNode parent = q.poll();

if (!nodes[i].equals("#")) {
/** NOTE !!! if node[i] is NOT null ( nodes[i] != "#" ) */
if (! nodes[i].equals("#")) {
TreeNode leftNode = new TreeNode(Integer.parseInt(nodes[i]));
parent.left = leftNode;
q.add(leftNode);
}

if (!nodes[++i].equals("#")) {
/** NOTE !!! if node[++i] is NOT null ( node[++i] != "#" ) */
if (! nodes[++i].equals("#")) {
TreeNode rightNode = new TreeNode(Integer.parseInt(nodes[i]));
parent.right = rightNode;
q.add(rightNode);
Expand Down

0 comments on commit d492a73

Please sign in to comment.