Skip to content

Commit 3e1d78a

Browse files
committed
add 776 java, progress
1 parent 9b226f7 commit 3e1d78a

File tree

6 files changed

+248
-21
lines changed

6 files changed

+248
-21
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@
899899
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)
900900
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*
901901
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)
902-
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***)
902+
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)
903903
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*
904904
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)
905905
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)

data/progress.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Progress
22

3+
# 2024-11-22
4+
- https://github.com/yennanliu/CS_basics/blob/master/doc/Leetcode_company_frequency-master/Google%206months-%20LeetCode.pdf
5+
36
# 2024-11-21
47
- https://github.com/wisdompeak/LeetCode/tree/master?tab=readme-ov-file#two-pointers
58
- (Two pointers for two sequences, Binary Search)

data/progress.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
20241122: 776
12
20241121: 004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)
23
20241120: 986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)
34
20241117: 394

data/to_review.txt

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
2025-01-14 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1+
2025-01-15 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
2+
2025-01-14 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
23
2025-01-11 -> ['394']
34
2025-01-10 -> ['833,950']
45
2025-01-04 -> ['53,210,207']
56
2025-01-03 -> ['444']
67
2025-01-02 -> ['1188,130,855(again)']
78
2024-12-28 -> ['900']
89
2024-12-27 -> ['26,27', '802,1197,26']
9-
2024-12-24 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
10+
2024-12-25 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
11+
2024-12-24 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1012
2024-12-21 -> ['394', '855,846']
1113
2024-12-20 -> ['833,950', '932']
1214
2024-12-18 -> ['951,792']
1315
2024-12-14 -> ['53,210,207', '163,1048']
1416
2024-12-13 -> ['444', '298,729']
15-
2024-12-12 -> ['1188,130,855(again)', '1146']
16-
2024-12-11 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
17+
2024-12-12 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '1188,130,855(again)', '1146']
18+
2024-12-11 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
1719
2024-12-08 -> ['394', '737']
1820
2024-12-07 -> ['833,950', '900', '686,734,737']
1921
2024-12-06 -> ['26,27', '802,1197,26', '353']
2022
2024-12-05 -> ['528,334']
21-
2024-12-03 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
23+
2024-12-04 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)']
24+
2024-12-03 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '1145']
2225
2024-12-01 -> ['53,210,207']
2326
2024-11-30 -> ['394', '444', '855,846', '1145,1219']
24-
2024-11-29 -> ['833,950', '1188,130,855(again)', '932']
25-
2024-11-28 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
27+
2024-11-29 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '1188,130,855(again)', '932']
28+
2024-11-28 -> ['986(todo),1229(todo),1868(todo),80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)']
2629
2024-11-27 -> ['951,792', '524,221,889']
27-
2024-11-26 -> ['743,889']
28-
2024-11-25 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '837']
29-
2024-11-24 -> ['833,950', '900']
30-
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']
31-
2024-11-22 -> ['986,80(todo),209(todo),283(todo),360(todo),713(todo),532(todo),611(todo)', '394', '444', '298,729', '1087']
32-
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']
30+
2024-11-26 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '743,889']
31+
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']
32+
2024-11-24 -> ['004(todo),153(todo),33(todo),81(todo),34(todo),162(todo),275(todo)', '833,950', '900']
33+
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']
34+
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']
35+
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']
3336
2024-11-20 -> ['394', '939']
3437
2024-11-19 -> ['394', '833,950']
3538
2024-11-18 -> ['394', '833,950', '53,210,207', '430']
@@ -844,7 +847,7 @@
844847
2020-07-23 -> ['388']
845848
2020-07-22 -> ['269']
846849
2020-07-21 -> ['646', '537,553,579']
847-
2020-07-20 -> ['210,261', '776']
850+
2020-07-20 -> ['776', '210,261']
848851
2020-07-19 -> ['066,271', '361,393,133,207', '355,119,536']
849852
2020-07-18 -> ['734,737', '771,775', '451,414,189']
850853
2020-07-16 -> ['163', '762', '725,538,089']
@@ -856,26 +859,26 @@
856859
2020-07-10 -> ['734,737', '388', '836,860,863']
857860
2020-07-09 -> ['066,271', '694']
858861
2020-07-08 -> ['066,271', '163', '646', '663']
859-
2020-07-07 -> ['066,271', '210,261', '298', '776', '661,662', '703,787,819']
862+
2020-07-07 -> ['776', '066,271', '210,261', '298', '661,662', '703,787,819']
860863
2020-07-06 -> ['130', '163', '361,393,133,207', '669,682,739,763']
861864
2020-07-05 -> ['163', '734,737', '346,686', '771,775', '701,450', '642,652,657']
862865
2020-07-04 -> ['163', '210,261', '640,645', '545,617,628']
863866
2020-07-03 -> ['361,393,133,207', '482,127,102,107', '762', '606,459']
864867
2020-07-02 -> ['210,261', '734,737', '298', '388', '742', '445,508']
865868
2020-07-01 -> ['130', '269', '210,261', '361,393,133,207', '322,380,394']
866869
2020-06-30 -> ['210,261', '361,393,133,207', '734,737', '346,686', '646', '738', '537,553,579', '289,295']
867-
2020-06-29 -> ['361,393,133,207', '734,737', '298', '776', '396', '251,287']
870+
2020-06-29 -> ['776', '361,393,133,207', '734,737', '298', '396', '251,287']
868871
2020-06-28 -> ['130', '734,737', '482,127,102,107', '355,119,536', '231,232,240']
869872
2020-06-27 -> ['298', '346,686', '388', '771,775', '451,414,189', '198,203,230']
870873
2020-06-26 -> ['130', '298', '694', '155,165,167,179']
871874
2020-06-25 -> ['130', '298', '346,686', '482,127,102,107', '646', '762', '663', '725,538,089']
872-
2020-06-24 -> ['130', '346,686', '388', '776', '742', '661,662', '517,535,529', '153']
875+
2020-06-24 -> ['776', '130', '346,686', '388', '742', '661,662', '517,535,529', '153']
873876
2020-06-23 -> ['346,686', '482,127,102,107', '711,046,126,127', '131,134,150']
874877
2020-06-22 -> ['482,127,102,107', '388', '646', '771,775', '738', '701,450', '063,064,120,0146']
875-
2020-06-21 -> ['482,127,102,107', '388', '776', '396', '640,645', '675,297,138']
878+
2020-06-21 -> ['776', '482,127,102,107', '388', '396', '640,645', '675,297,138']
876879
2020-06-20 -> ['388', '646', '762', '606,459']
877-
2020-06-19 -> ['646', '776', '771,775', '742', '836,860,863']
878-
2020-06-18 -> ['269', '646', '776', '694']
880+
2020-06-19 -> ['776', '646', '771,775', '742', '836,860,863']
881+
2020-06-18 -> ['776', '269', '646', '694']
879882
2020-06-17 -> ['776', '771,775', '762', '738', '663', '537,553,579']
880883
2020-06-16 -> ['771,775', '742', '396', '661,662', '703,787,819']
881884
2020-06-15 -> ['771,775', '762', '355,119,536', '669,682,739,763']
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
package LeetCodeJava.BinarySearchTree;
2+
3+
// https://leetcode.com/problems/split-bst/description/
4+
// https://leetcode.ca/all/776.html
5+
6+
import LeetCodeJava.DataStructure.TreeNode;
7+
8+
/**
9+
* 776. Split BST
10+
* 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.
11+
*
12+
* 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.
13+
*
14+
* You should output the root TreeNode of both subtrees after splitting, in any order.
15+
*
16+
* Example 1:
17+
*
18+
* Input: root = [4,2,6,1,3,5,7], V = 2
19+
* Output: [[2,1],[4,3,6,null,null,5,7]]
20+
* Explanation:
21+
* Note that root, output[0], and output[1] are TreeNode objects, not arrays.
22+
*
23+
* The given tree [4,2,6,1,3,5,7] is represented by the following diagram:
24+
*
25+
* 4
26+
* / \
27+
* 2 6
28+
* / \ / \
29+
* 1 3 5 7
30+
*
31+
* while the diagrams for the outputs are:
32+
*
33+
* 4
34+
* / \
35+
* 3 6 and 2
36+
* / \ /
37+
* 5 7 1
38+
* Note:
39+
*
40+
* The size of the BST will not exceed 50.
41+
* The BST is always valid and each node's value is different.
42+
* Difficulty:
43+
* Medium
44+
* Lock:
45+
* Prime
46+
* Company:
47+
* Amazon Coupang Google
48+
*
49+
*/
50+
public class SplitBST {
51+
52+
/**
53+
* Definition for a binary tree node.
54+
* public class TreeNode {
55+
* int val;
56+
* TreeNode left;
57+
* TreeNode right;
58+
* TreeNode() {}
59+
* TreeNode(int val) { this.val = val; }
60+
* TreeNode(int val, TreeNode left, TreeNode right) {
61+
* this.val = val;
62+
* this.left = left;
63+
* this.right = right;
64+
* }
65+
* }
66+
*/
67+
68+
// V0
69+
// TODO : implement
70+
// public TreeNode[] splitBST(TreeNode root, int target) {
71+
// }
72+
73+
// V1
74+
// IDEA : DFS
75+
// https://leetcode.ca/2018-01-14-776-Split-BST/
76+
private int t;
77+
78+
public TreeNode[] splitBST_1(TreeNode root, int target) {
79+
t = target;
80+
return dfs(root);
81+
}
82+
83+
private TreeNode[] dfs(TreeNode root) {
84+
if (root == null) {
85+
return new TreeNode[] {null, null};
86+
}
87+
if (root.val <= t) {
88+
TreeNode[] ans = dfs(root.right);
89+
root.right = ans[0];
90+
ans[0] = root;
91+
return ans;
92+
} else {
93+
TreeNode[] ans = dfs(root.left);
94+
root.left = ans[1];
95+
ans[1] = root;
96+
return ans;
97+
}
98+
}
99+
100+
// V2
101+
// IDEA : DFS (gpt)
102+
// TODO : validate
103+
/**
104+
* Explanation
105+
*
106+
* 1. Base Case:
107+
* • If the root is null, return two null subtrees because there’s nothing to split.
108+
*
109+
* 2. Recursive Case:
110+
* • If the root’s value is <= V :
111+
* • Keep the root as part of the left subtree.
112+
* • Recursively split the right child since nodes greater than V might be in the right subtree.
113+
*
114+
* • If the root’s value is > V :
115+
* • Keep the root as part of the right subtree.
116+
* • Recursively split the left child since nodes <= V might be in the left subtree.
117+
* 3. Reassign Children:
118+
* • After the recursive split, adjust the child pointers of the root node to maintain the BST structure.
119+
* 4. Result:
120+
* • The first element in the result array is the root of the subtree <= V .
121+
* • The second element is the root of the subtree > V .
122+
*
123+
*
124+
* Time Complexity
125+
*
126+
* • Each node is visited exactly once, so the time complexity is O(n) , where n is the number of nodes in the BST.
127+
*
128+
* Space Complexity
129+
*
130+
* • The space complexity is O(h) , where h is the height of the tree, due to the recursive stack.
131+
*
132+
*/
133+
public TreeNode[] splitBST_2(TreeNode root, int V) {
134+
// Base case: If the root is null, both subtrees are null
135+
if (root == null) {
136+
return new TreeNode[]{null, null};
137+
}
138+
139+
// Array to hold the two resulting subtrees
140+
TreeNode[] result = new TreeNode[2];
141+
142+
// If the root's value is less than or equal to V
143+
if (root.val <= V) {
144+
// Recursively split the right subtree
145+
TreeNode[] splitRight = splitBST_2(root.right, V);
146+
147+
// Root becomes the head of the left subtree
148+
result[0] = root;
149+
root.right = splitRight[0]; // Reassign the right child to the left subtree
150+
result[1] = splitRight[1]; // Right subtree from the split becomes the second result
151+
} else {
152+
// If root's value is greater than V, recursively split the left subtree
153+
TreeNode[] splitLeft = splitBST_2(root.left, V);
154+
155+
// Root becomes the head of the right subtree
156+
result[1] = root;
157+
root.left = splitLeft[1]; // Reassign the left child to the right subtree
158+
result[0] = splitLeft[0]; // Left subtree from the split becomes the first result
159+
}
160+
161+
return result;
162+
}
163+
164+
// V3
165+
// https://www.cnblogs.com/grandyang/p/8993143.html
166+
167+
168+
}

leetcode_java/src/main/java/dev/workspace5.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,6 +3469,58 @@ private String getMultiplyStr(String cur, Integer multiply) {
34693469
return sb.toString();
34703470
}
34713471

3472+
// LC 776
3473+
// https://leetcode.ca/2018-01-14-776-Split-BST/
3474+
// https://leetcode.ca/all/776.html
3475+
// 10.09 am - 10.20 am
3476+
/**
3477+
* Idea : split tree
3478+
*
3479+
* -> split tree into smaller, bigger array
3480+
* -> then build BST again from 2 arrays above
3481+
*/
3482+
public TreeNode[] splitBST(TreeNode root, int target) {
3483+
3484+
// split to 2 arrays
3485+
List<Integer> smaller = new ArrayList<>();
3486+
List<Integer> bigger = new ArrayList<>();
3487+
3488+
Queue<TreeNode> queue = new LinkedList<>();
3489+
queue.add(root);
3490+
3491+
// bfs
3492+
while(!queue.isEmpty()){
3493+
// update array
3494+
TreeNode cur = queue.poll();
3495+
if (cur.val <= target){
3496+
smaller.add(cur.val);
3497+
}else{
3498+
bigger.add(cur.val);
3499+
}
3500+
// add to queue
3501+
if (cur.left != null){
3502+
queue.add(cur.left);
3503+
}
3504+
if (cur.right != null){
3505+
queue.add(cur.right);
3506+
}
3507+
}
3508+
3509+
// build tree
3510+
TreeNode smallerTree = buildTree(smaller);
3511+
TreeNode biggerTree = buildTree(bigger);
3512+
3513+
// collect result
3514+
TreeNode[] res = new TreeNode[2];
3515+
res[0] = smallerTree;
3516+
res[1] = biggerTree;
3517+
3518+
return res;
3519+
}
3520+
3521+
private TreeNode buildTree(List<Integer> input){
3522+
return null;
3523+
}
34723524

34733525
}
34743526

0 commit comments

Comments
 (0)