Skip to content

Commit

Permalink
udpate 226 java, cheatsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 10, 2024
1 parent be32584 commit 15eb825
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 13 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240310: 213,217
20240310: 213,217,226
20240309: 207(again!!!),208,211(again),212
20240308: 152,153(again),190,191,198,200,206(again)
20240307: 139(again),141,143(again)
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
2024-05-04 -> ['213,217']
2024-05-04 -> ['213,217,226']
2024-05-03 -> ['207(again!!!),208,211(again),212']
2024-05-02 -> ['152,153(again),190,191,198,200,206(again)']
2024-05-01 -> ['139(again),141,143(again)']
Expand All @@ -9,7 +9,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-13 -> ['213,217']
2024-04-13 -> ['213,217,226']
2024-04-12 -> ['207(again!!!),208,211(again),212']
2024-04-11 -> ['152,153(again),190,191,198,200,206(again)']
2024-04-10 -> ['139(again),141,143(again)']
Expand All @@ -20,27 +20,27 @@
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-02 -> ['20,21,23,33(again)']
2024-04-01 -> ['1,3,5,4,19']
2024-03-31 -> ['213,217']
2024-03-31 -> ['213,217,226']
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-24 -> ['55(again),56,62,70']
2024-03-23 -> ['213,217']
2024-03-23 -> ['213,217,226']
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-19 -> ['121,124(again),125,128,133(again)', '1,3,5,4,19']
2024-03-18 -> ['213,217', '100,102,104,105(again)']
2024-03-18 -> ['213,217,226', '100,102,104,105(again)']
2024-03-17 -> ['207(again!!!),208,211(again),212', '73,76,79(again),91,25']
2024-03-16 -> ['152,153(again),190,191,198,200,206(again)', '55(again),56,62,70']
2024-03-15 -> ['213,217', '139(again),141,143(again)']
2024-03-15 -> ['213,217,226', '139(again),141,143(again)']
2024-03-14 -> ['207(again!!!),208,211(again),212', '121,124(again),125,128,133(again)']
2024-03-13 -> ['213,217', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['213,217', '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', '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-13 -> ['213,217,226', '152,153(again),190,191,198,200,206(again)', '100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['213,217,226', '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', '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)']
2024-03-09 -> ['152,153(again),190,191,198,200,206(again)', '139(again),141,143(again)', '121,124(again),125,128,133(again)', '73,76,79(again),91,25']
2024-03-08 -> ['139(again),141,143(again)', '121,124(again),125,128,133(again)', '100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54']
Expand Down
40 changes: 40 additions & 0 deletions doc/cheatsheet/tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,46 @@ class Solution(object):
return root
```

```java
// java
// DFS
// V0
// IDEA : DFS
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
/** NOTE !!!!
*
* instead of calling invertTree and assign value to sub tree directly,
* we need to CACHE invertTree result, and assign later
* -> since assign directly will cause tree changed, and affect the other invertTree call
*
* e.g. below is WRONG,
* root.left = invertTree(root.right);
* root.right = invertTree(root.left);
*
* need to cache result
*
* TreeNode left = invertTree(root.left);
* TreeNode right = invertTree(root.right);
*
* then assign to sub tree
*
* root.left = right;
* root.right = left;
*/
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
/** NOTE !!!! below is WRONG */
// root.left = invertTree(root.right);
// root.right = invertTree(root.left);
return root;
}
```

### 1-1-16) Get neighborhood node (for each node) from tree
```python
# LC 863. All Nodes Distance K in Binary Tree
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,44 @@
public class InvertBinaryTree {

// V0
// IDEA : BFS
// IDEA : DFS
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
/** NOTE !!!!
*
* instead of calling invertTree and assign value to sub tree directly,
* we need to CACHE invertTree result, and assign later
* -> since assign directly will cause tree changed, and affect the other invertTree call
*
* e.g. below is WRONG,
* root.left = invertTree(root.right);
* root.right = invertTree(root.left);
*
* need to cache result
*
* TreeNode left = invertTree(root.left);
* TreeNode right = invertTree(root.right);
*
* then assign to sub tree
*
* root.left = right;
* root.right = left;
*/
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.left = right;
root.right = left;
/** NOTE !!!! below is WRONG */
// root.left = invertTree(root.right);
// root.right = invertTree(root.left);
return root;
}

// V0
// IDEA : BFS
public TreeNode invertTree_(TreeNode root) {

if (root == null) {
return null;
Expand Down Expand Up @@ -48,8 +84,8 @@ public TreeNode invertTree_2(TreeNode root) {
if (root == null) {
return null;
}
TreeNode right = invertTree(root.right);
TreeNode left = invertTree(root.left);
TreeNode right = invertTree_2(root.right);
TreeNode left = invertTree_2(root.left);
root.left = right;
root.right = left;
return root;
Expand Down

0 comments on commit 15eb825

Please sign in to comment.