Skip to content

Commit

Permalink
add 776 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 22, 2024
1 parent 9b226f7 commit 3e1d78a
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@
450|[Delete Node in a BST](https://leetcode.com/problems/delete-node-in-a-bst/)| [Python](./leetcode_python/Binary_Search_Tree/delete-node-in-a-bst.py) | _O(h)_ | _O(h)_ | Medium | AGAIN, good trick, BST, tree, dfs | AGAIN****** (6) (but again)
530|[Minimum Absolute Difference in BST](https://leetcode.com/problems/minimum-absolute-difference-in-bst/)| [Python](./leetcode_python/Binary_Search_Tree/minimum-absolute-difference-in-bst.py) | _O(n)_ | _O(h)_ | Easy | | AGAIN*
700|[Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/)| [Python](./leetcode_python/Binary_Search_Tree/search-in-a-binary-search-tree.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearchTree/SearchInABinarySearchTree.java) | _O(n)_ | _O(h)_ | Easy |BST, good basic, recursion, iteration amazon | AGAIN*** (1)
776|[Split BST](https://leetcode.com/problems/split-bst/)| [Python](./leetcode_python/Binary_Search_Tree/split-bst.py) | _O(n)_ | _O(h)_ | Medium | 🔒, BST, tree, DFS, trick, basic, AGAIN ,`amazon` | AGAIN******* (5) (not start***)
776|[Split BST](https://leetcode.com/problems/split-bst/)| [Python](./leetcode_python/Binary_Search_Tree/split-bst.py), [Java](./leetcode_java/src/main/java/LeetCodeJava/BinarySearchTree/SplitBST.java) | _O(n)_ | _O(h)_ | Medium | 🔒, BST, DFS, trick, AGAIN ,`amazon`, google|AGAIN********* (6)
783|[Minimum Distance Between BST Nodes](https://leetcode.com/problems/minimum-distance-between-bst-nodes/)| [Python](./leetcode_python/Binary_Search_Tree/minimum-distance-between-bst-nodes.py) | _O(n)_ | _O(h)_ | Easy | | OK*
426|[Convert Binary Search Tree to Sorted Doubly Linked List](https://leetcode.com/problems/convert-binary-search-tree-to-sorted-doubly-linked-list/)| [Python](./leetcode_python/Binary_Search_Tree/convert_binary_search_tree_to_sorted_doubly_linked_list.py) ||| Medium |recursion, tree, `linked list`, `good basic` `UBER`, `lyft`, `amazon`, `google`,`fb`| AGAIN******* (5) (not start)
968| [Binary Tree Cameras](https://leetcode.com/problems/binary-tree-cameras/) | [Python](./leetcode_python/Binary_Search_Tree/binary-tree-cameras.py)| | _O(n)_ | Hard|`amazon`, dfs, bst, dp, greedy, m$| AGAIN (not start)
Expand Down
3 changes: 3 additions & 0 deletions data/progress.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Progress

# 2024-11-22
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf

# 2024-11-21
- https://github.com/wisdompeak/LeetCode/tree/master?tab=readme-ov-file#two-pointers
- (Two pointers for two sequences, Binary Search)
Expand Down
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20241122: 776
20241121: 004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)
20241120: 986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
20241117: 394
Expand Down
43 changes: 23 additions & 20 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,35 +1,38 @@
2025-01-14 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-15 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2025-01-11 -> ['394']
2025-01-10 -> ['833,950']
2025-01-04 -> ['53,210,207']
2025-01-03 -> ['444']
2025-01-02 -> ['1188,130,855(again)']
2024-12-28 -> ['900']
2024-12-27 -> ['26,27', '802,1197,26']
2024-12-24 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-25 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-21 -> ['394', '855,846']
2024-12-20 -> ['833,950', '932']
2024-12-18 -> ['951,792']
2024-12-14 -> ['53,210,207', '163,1048']
2024-12-13 -> ['444', '298,729']
2024-12-12 -> ['1188,130,855(again)', '1146']
2024-12-11 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-12 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-12-08 -> ['394', '737']
2024-12-07 -> ['833,950', '900', '686,734,737']
2024-12-06 -> ['26,27', '802,1197,26', '353']
2024-12-05 -> ['528,334']
2024-12-03 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2024-12-04 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2024-12-03 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2024-12-01 -> ['53,210,207']
2024-11-30 -> ['394', '444', '855,846', '1145,1219']
2024-11-29 -> ['833,950', '1188,130,855(again)', '932']
2024-11-28 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-29 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
2024-11-28 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2024-11-27 -> ['951,792', '524,221,889']
2024-11-26 -> ['743,889']
2024-11-25 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '837']
2024-11-24 -> ['833,950', '900']
2024-11-23 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '53,210,207', '26,27', '802,1197,26', '163,1048', '981']
2024-11-22 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '444', '298,729', '1087']
2024-11-21 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '833,950', '1188,130,855(again)', '1146']
2024-11-26 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
2024-11-25 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '837']
2024-11-24 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
2024-11-23 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '53,210,207', '26,27', '802,1197,26', '163,1048', '981']
2024-11-22 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '444', '298,729', '1087']
2024-11-21 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '833,950', '1188,130,855(again)', '1146']
2024-11-20 -> ['394', '939']
2024-11-19 -> ['394', '833,950']
2024-11-18 -> ['394', '833,950', '53,210,207', '430']
Expand Down Expand Up @@ -844,7 +847,7 @@
2020-07-23 -> ['388']
2020-07-22 -> ['269']
2020-07-21 -> ['646', '537,553,579']
2020-07-20 -> ['210,261', '776']
2020-07-20 -> ['776', '210,261']
2020-07-19 -> ['066,271', '361,393,133,207', '355,119,536']
2020-07-18 -> ['734,737', '771,775', '451,414,189']
2020-07-16 -> ['163', '762', '725,538,089']
Expand All @@ -856,26 +859,26 @@
2020-07-10 -> ['734,737', '388', '836,860,863']
2020-07-09 -> ['066,271', '694']
2020-07-08 -> ['066,271', '163', '646', '663']
2020-07-07 -> ['066,271', '210,261', '298', '776', '661,662', '703,787,819']
2020-07-07 -> ['776', '066,271', '210,261', '298', '661,662', '703,787,819']
2020-07-06 -> ['130', '163', '361,393,133,207', '669,682,739,763']
2020-07-05 -> ['163', '734,737', '346,686', '771,775', '701,450', '642,652,657']
2020-07-04 -> ['163', '210,261', '640,645', '545,617,628']
2020-07-03 -> ['361,393,133,207', '482,127,102,107', '762', '606,459']
2020-07-02 -> ['210,261', '734,737', '298', '388', '742', '445,508']
2020-07-01 -> ['130', '269', '210,261', '361,393,133,207', '322,380,394']
2020-06-30 -> ['210,261', '361,393,133,207', '734,737', '346,686', '646', '738', '537,553,579', '289,295']
2020-06-29 -> ['361,393,133,207', '734,737', '298', '776', '396', '251,287']
2020-06-29 -> ['776', '361,393,133,207', '734,737', '298', '396', '251,287']
2020-06-28 -> ['130', '734,737', '482,127,102,107', '355,119,536', '231,232,240']
2020-06-27 -> ['298', '346,686', '388', '771,775', '451,414,189', '198,203,230']
2020-06-26 -> ['130', '298', '694', '155,165,167,179']
2020-06-25 -> ['130', '298', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089']
2020-06-24 -> ['130', '346,686', '388', '776', '742', '661,662', '517,535,529', '153']
2020-06-24 -> ['776', '130', '346,686', '388', '742', '661,662', '517,535,529', '153']
2020-06-23 -> ['346,686', '482,127,102,107', '711,046,126,127', '131,134,150']
2020-06-22 -> ['482,127,102,107', '388', '646', '771,775', '738', '701,450', '063,064,120,0146']
2020-06-21 -> ['482,127,102,107', '388', '776', '396', '640,645', '675,297,138']
2020-06-21 -> ['776', '482,127,102,107', '388', '396', '640,645', '675,297,138']
2020-06-20 -> ['388', '646', '762', '606,459']
2020-06-19 -> ['646', '776', '771,775', '742', '836,860,863']
2020-06-18 -> ['269', '646', '776', '694']
2020-06-19 -> ['776', '646', '771,775', '742', '836,860,863']
2020-06-18 -> ['776', '269', '646', '694']
2020-06-17 -> ['776', '771,775', '762', '738', '663', '537,553,579']
2020-06-16 -> ['771,775', '742', '396', '661,662', '703,787,819']
2020-06-15 -> ['771,775', '762', '355,119,536', '669,682,739,763']
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package LeetCodeJava.BinarySearchTree;

// https://leetcode.com/problems/split-bst/description/
// https://leetcode.ca/all/776.html

import LeetCodeJava.DataStructure.TreeNode;

/**
* 776. Split BST
* Given a Binary Search Tree (BST) with root node root, and a target value V, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It's not necessarily the case that the tree contains a node with value V.
*
* Additionally, most of the structure of the original tree should remain. Formally, for any child C with parent P in the original tree, if they are both in the same subtree after the split, then node C should still have the parent P.
*
* You should output the root TreeNode of both subtrees after splitting, in any order.
*
* Example 1:
*
* Input: root = [4,2,6,1,3,5,7], V = 2
* Output: [[2,1],[4,3,6,null,null,5,7]]
* Explanation:
* Note that root, output[0], and output[1] are TreeNode objects, not arrays.
*
* The given tree [4,2,6,1,3,5,7] is represented by the following diagram:
*
* 4
* / \
* 2 6
* / \ / \
* 1 3 5 7
*
* while the diagrams for the outputs are:
*
* 4
* / \
* 3 6 and 2
* / \ /
* 5 7 1
* Note:
*
* The size of the BST will not exceed 50.
* The BST is always valid and each node's value is different.
* Difficulty:
* Medium
* Lock:
* Prime
* Company:
* Amazon Coupang Google
*
*/
public class SplitBST {

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/

// V0
// TODO : implement
// public TreeNode[] splitBST(TreeNode root, int target) {
// }

// V1
// IDEA : DFS
// https://leetcode.ca/2018-01-14-776-Split-BST/
private int t;

public TreeNode[] splitBST_1(TreeNode root, int target) {
t = target;
return dfs(root);
}

private TreeNode[] dfs(TreeNode root) {
if (root == null) {
return new TreeNode[] {null, null};
}
if (root.val <= t) {
TreeNode[] ans = dfs(root.right);
root.right = ans[0];
ans[0] = root;
return ans;
} else {
TreeNode[] ans = dfs(root.left);
root.left = ans[1];
ans[1] = root;
return ans;
}
}

// V2
// IDEA : DFS (gpt)
// TODO : validate
/**
* Explanation
*
* 1. Base Case:
* • If the root is null, return two null subtrees because there’s nothing to split.
*
* 2. Recursive Case:
* • If the root’s value is <= V :
* • Keep the root as part of the left subtree.
* • Recursively split the right child since nodes greater than V might be in the right subtree.
*
* • If the root’s value is > V :
* • Keep the root as part of the right subtree.
* • Recursively split the left child since nodes <= V might be in the left subtree.
* 3. Reassign Children:
* • After the recursive split, adjust the child pointers of the root node to maintain the BST structure.
* 4. Result:
* • The first element in the result array is the root of the subtree <= V .
* • The second element is the root of the subtree > V .
*
*
* Time Complexity
*
* • Each node is visited exactly once, so the time complexity is O(n) , where n is the number of nodes in the BST.
*
* Space Complexity
*
* • The space complexity is O(h) , where h is the height of the tree, due to the recursive stack.
*
*/
public TreeNode[] splitBST_2(TreeNode root, int V) {
// Base case: If the root is null, both subtrees are null
if (root == null) {
return new TreeNode[]{null, null};
}

// Array to hold the two resulting subtrees
TreeNode[] result = new TreeNode[2];

// If the root's value is less than or equal to V
if (root.val <= V) {
// Recursively split the right subtree
TreeNode[] splitRight = splitBST_2(root.right, V);

// Root becomes the head of the left subtree
result[0] = root;
root.right = splitRight[0]; // Reassign the right child to the left subtree
result[1] = splitRight[1]; // Right subtree from the split becomes the second result
} else {
// If root's value is greater than V, recursively split the left subtree
TreeNode[] splitLeft = splitBST_2(root.left, V);

// Root becomes the head of the right subtree
result[1] = root;
root.left = splitLeft[1]; // Reassign the left child to the right subtree
result[0] = splitLeft[0]; // Left subtree from the split becomes the first result
}

return result;
}

// V3
// https://www.cnblogs.com/grandyang/p/8993143.html


}
52 changes: 52 additions & 0 deletions leetcode_java/src/main/java/dev/workspace5.java
Original file line number Diff line number Diff line change
Expand Up @@ -3469,6 +3469,58 @@ private String getMultiplyStr(String cur, Integer multiply) {
return sb.toString();
}

// LC 776
// https://leetcode.ca/2018-01-14-776-Split-BST/
// https://leetcode.ca/all/776.html
// 10.09 am - 10.20 am
/**
* Idea : split tree
*
* -> split tree into smaller, bigger array
* -> then build BST again from 2 arrays above
*/
public TreeNode[] splitBST(TreeNode root, int target) {

// split to 2 arrays
List<Integer> smaller = new ArrayList<>();
List<Integer> bigger = new ArrayList<>();

Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);

// bfs
while(!queue.isEmpty()){
// update array
TreeNode cur = queue.poll();
if (cur.val <= target){
smaller.add(cur.val);
}else{
bigger.add(cur.val);
}
// add to queue
if (cur.left != null){
queue.add(cur.left);
}
if (cur.right != null){
queue.add(cur.right);
}
}

// build tree
TreeNode smallerTree = buildTree(smaller);
TreeNode biggerTree = buildTree(bigger);

// collect result
TreeNode[] res = new TreeNode[2];
res[0] = smallerTree;
res[1] = biggerTree;

return res;
}

private TreeNode buildTree(List<Integer> input){
return null;
}

}

Expand Down

0 comments on commit 3e1d78a

Please sign in to comment.