Skip to content

Commit b8808cc

Browse files
committed
update
1 parent 678e462 commit b8808cc

File tree

1 file changed

+65
-32
lines changed

1 file changed

+65
-32
lines changed

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

Lines changed: 65 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3472,52 +3472,85 @@ private String getMultiplyStr(String cur, Integer multiply) {
34723472
// LC 776
34733473
// https://leetcode.ca/2018-01-14-776-Split-BST/
34743474
// https://leetcode.ca/all/776.html
3475-
// 10.09 am - 10.20 am
3475+
// 8.37 PM - 8.50 PM
34763476
/**
34773477
* Idea : split tree
34783478
*
34793479
* -> split tree into smaller, bigger array
34803480
* -> then build BST again from 2 arrays above
34813481
*/
3482+
3483+
// IDEA : DFS
3484+
TreeNode leftTree = new TreeNode();
3485+
TreeNode rightTree = new TreeNode();
34823486
public TreeNode[] splitBST(TreeNode root, int target) {
3487+
TreeNode[] res = new TreeNode[2];
34833488

3484-
// split to 2 arrays
3485-
List<Integer> smaller = new ArrayList<>();
3486-
List<Integer> bigger = new ArrayList<>();
3489+
buildTreeDfs(root, target);
34873490

3488-
Queue<TreeNode> queue = new LinkedList<>();
3489-
queue.add(root);
3491+
res[0] = leftTree;
3492+
res[1] = rightTree; // ??
3493+
return res;
3494+
}
34903495

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-
}
3496+
private TreeNode[] buildTreeDfs(TreeNode root, int target){
3497+
if (root == null){
3498+
return null;
35073499
}
3500+
// if (root.val <= target){
3501+
// // ??
3502+
// this.buildTreeDfs(leftTree, target);
3503+
// }else{
3504+
// // ?? or return
3505+
// this.buildTreeDfs(rightTree, target);
3506+
// }
3507+
if (root.left != null && root.left.val <= target){
35083508

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;
3509+
leftTree.left = root.left;
3510+
buildTreeDfs(leftTree, target);
3511+
}
3512+
return null;
35193513
}
35203514

3515+
// public TreeNode[] splitBST(TreeNode root, int target) {
3516+
//
3517+
// // split to 2 arrays
3518+
// List<Integer> smaller = new ArrayList<>();
3519+
// List<Integer> bigger = new ArrayList<>();
3520+
//
3521+
// Queue<TreeNode> queue = new LinkedList<>();
3522+
// queue.add(root);
3523+
//
3524+
// // bfs
3525+
// while(!queue.isEmpty()){
3526+
// // update array
3527+
// TreeNode cur = queue.poll();
3528+
// if (cur.val <= target){
3529+
// smaller.add(cur.val);
3530+
// }else{
3531+
// bigger.add(cur.val);
3532+
// }
3533+
// // add to queue
3534+
// if (cur.left != null){
3535+
// queue.add(cur.left);
3536+
// }
3537+
// if (cur.right != null){
3538+
// queue.add(cur.right);
3539+
// }
3540+
// }
3541+
//
3542+
// // build tree
3543+
// TreeNode smallerTree = buildTree(smaller);
3544+
// TreeNode biggerTree = buildTree(bigger);
3545+
//
3546+
// // collect result
3547+
// TreeNode[] res = new TreeNode[2];
3548+
// res[0] = smallerTree;
3549+
// res[1] = biggerTree;
3550+
//
3551+
// return res;
3552+
// }
3553+
35213554
private TreeNode buildTree(List<Integer> input){
35223555
return null;
35233556
}

0 commit comments

Comments
 (0)