Skip to content

Commit

Permalink
update 572 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 22, 2024
1 parent 36bc41a commit 03ea7f8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@
545 | [Boundary of Binary Tree](https://leetcode.com/problems/boundary-of-binary-tree/) | [Python](./leetcode_python/Tree/boundary-of-binary-tree.py) | _O(n)_ | _O(h)_ | Medium |recursive, dfs, tree boundary, tree,🔒, `good trick`, `amazon`| AGAIN********* (6)
548 | [Split Array with Equal Sum](https://leetcode.com/problems/split-array-with-equal-sum/) |[Python](./leetcode_python/Tree/split-array-with-equal-sum.py) | _O(n^2)_ | _O(n)_ | Medium |🔒, `good trick`, `array`| AGAIN*** (2)
563 |[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/)| [Python](./leetcode_python/Tree/binary-tree-tilt.py)| _O(n)_ | _O(n)_ | Easy | | AGAIN
572 |[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)| [Python](./leetcode_python/Tree/subtree-of-another-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Tree/SubtreeOfAnotherTree.java) | _O(m * n)_ | _O(h)_ | Easy | Curated Top 75, LC 100, recursive call recursion func, good basic, dfs, bfs, `amazon`,`fb`| OK************* (9) (but again)
572 |[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/)| [Python](./leetcode_python/Tree/subtree-of-another-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Tree/SubtreeOfAnotherTree.java) | _O(m * n)_ | _O(h)_ | Easy | Curated Top 75, LC 100, recursive call recursion func, good basic, dfs, bfs, `amazon`,`fb`| OK************* (10) (but again)
606 |[Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/)| [Python](./leetcode_python/Tree/construct-string-from-binary-tree.py) | _O(n)_ | _O(h)_ | Easy |good basic, tree, dfs, LC # 536, `amazon`| OK******** (7) (again!)
617 |[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Python](./leetcode_python/Tree/merge-two-binary-trees.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/Tree/MergeTwoBinaryTrees.java) | _O(n)_ | _O(h)_ | Easy | tree, dfs, bfs, `good basic`, `amazon`| OK************* (7) (but again)
623 |[Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/)| [Python](./leetcode_python/Tree/add-one-row-to-tree.py)| _O(n)_ | _O(h)_ | Medium |`good basic`, `dfs`, `bfs` | AGAIN** (2)
Expand Down
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240522: 62
20240522: 62,572
20240521: 322,191,190
20240520: 73,200,70,323(again)
20240519: 207,79,206,213,198
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-07-16 -> ['62']
2024-07-16 -> ['62,572']
2024-07-15 -> ['322,191,190']
2024-07-14 -> ['73,200,70,323(again)']
2024-07-13 -> ['207,79,206,213,198']
Expand All @@ -11,7 +11,7 @@
2024-07-06 -> ['371']
2024-07-05 -> ['121,252']
2024-07-04 -> ['125']
2024-06-25 -> ['62']
2024-06-25 -> ['62,572']
2024-06-24 -> ['322,191,190']
2024-06-23 -> ['73,200,70,323(again)']
2024-06-22 -> ['207,79,206,213,198']
Expand All @@ -24,27 +24,27 @@
2024-06-15 -> ['371']
2024-06-14 -> ['121,252']
2024-06-13 -> ['125']
2024-06-12 -> ['62']
2024-06-12 -> ['62,572']
2024-06-11 -> ['322,191,190']
2024-06-10 -> ['73,200,70,323(again)']
2024-06-09 -> ['207,79,206,213,198']
2024-06-08 -> ['212(todo),211,338,208(again)']
2024-06-07 -> ['347,253(todo),91(todo),217']
2024-06-06 -> ['226,98,253(todo)']
2024-06-05 -> ['104,230,102,100']
2024-06-04 -> ['62', '105,106']
2024-06-04 -> ['62,572', '105,106']
2024-06-03 -> ['322,191,190', '242,235']
2024-06-02 -> ['73,200,70,323(again)', '371']
2024-06-01 -> ['207,79,206,213,198', '121,252']
2024-05-31 -> ['212(todo),211,338,208(again)', '125']
2024-05-30 -> ['62', '347,253(todo),91(todo),217']
2024-05-30 -> ['62,572', '347,253(todo),91(todo),217']
2024-05-29 -> ['322,191,190', '226,98,253(todo)']
2024-05-28 -> ['73,200,70,323(again)', '104,230,102,100']
2024-05-27 -> ['62', '207,79,206,213,198', '105,106']
2024-05-27 -> ['62,572', '207,79,206,213,198', '105,106']
2024-05-26 -> ['322,191,190', '212(todo),211,338,208(again)', '242,235']
2024-05-25 -> ['62', '73,200,70,323(again)', '347,253(todo),91(todo),217', '371']
2024-05-24 -> ['62', '322,191,190', '207,79,206,213,198', '226,98,253(todo)', '121,252']
2024-05-23 -> ['62', '322,191,190', '73,200,70,323(again)', '212(todo),211,338,208(again)', '104,230,102,100', '125']
2024-05-25 -> ['62,572', '73,200,70,323(again)', '347,253(todo),91(todo),217', '371']
2024-05-24 -> ['62,572', '322,191,190', '207,79,206,213,198', '226,98,253(todo)', '121,252']
2024-05-23 -> ['62,572', '322,191,190', '73,200,70,323(again)', '212(todo),211,338,208(again)', '104,230,102,100', '125']
2024-05-22 -> ['322,191,190', '73,200,70,323(again)', '207,79,206,213,198', '347,253(todo),91(todo),217', '105,106']
2024-05-21 -> ['73,200,70,323(again)', '207,79,206,213,198', '212(todo),211,338,208(again)', '226,98,253(todo)', '242,235']
2024-05-20 -> ['207,79,206,213,198', '212(todo),211,338,208(again)', '347,253(todo),91(todo),217', '104,230,102,100', '371']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,39 @@ public boolean isSubtree(TreeNode s, TreeNode t) {
if (s == null || t == null) {
return false;
}
// NOTE !!! isSameTree and isSubtree with sub left, sub right tree
/**
* NOTE !!! isSameTree and isSubtree with sub left, sub right tree
*
* e.g.
* isSubtree(s.left, t)
* isSubtree(s.right, t)
*
* -> only take sub tree on s (root), but use the same t (sub root)
*
*
* NOTE !!!
* -> below is "or" logic
*/
return isSameTree(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);
}

/** NOTE !!!
*
* isSameTree func is comparing if s, t are the same tree (same structure, val)
*/
private boolean isSameTree(TreeNode s, TreeNode t) {

if (s == null && t == null) {
return true;
}
if (s == null || t == null) {
return false;
}
/**
* NOTE !!!
*
* -> below is "and" logic
*/
return s.val == t.val && isSameTree(s.left, t.left) && isSameTree(s.right, t.right);
}

Expand Down
39 changes: 39 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -1364,4 +1364,43 @@ public int get_factorial(int n){
return res;
}

// LC 572
// dfs
public boolean isSubtree(TreeNode root, TreeNode subRoot) {

if (root == null && subRoot.val == root.val){
return true;
}

if (root == null || subRoot.val == root.val){
return false;
}

//return false;
return this.isSameTree_(root, subRoot) ||
this.isSameTree_(root.left, subRoot) ||
this.isSameTree_(root.right, subRoot);
}

public boolean isSameTree_(TreeNode root, TreeNode subRoot){

System.out.println("root = " + root.val + " subRoot = " + subRoot.val);

if (root.val != subRoot.val){
return false;
}

if (root.left == null || subRoot.left == null){
return false;
}

if (root.right == null || subRoot.right == null){
return false;
}

return root.val == subRoot.val &&
this.isSameTree_(root.left, subRoot.left) &&
this.isSameTree_(root.right, subRoot.right);
}

}

0 comments on commit 03ea7f8

Please sign in to comment.