Skip to content

Commit

Permalink
update 105 java, py, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 14, 2024
1 parent 67deeb1 commit 12283d4
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 10 deletions.
1 change: 1 addition & 0 deletions data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
20240514: 105,106(todo)
20240513: 242,235
20240512: 371
20240511: 121,252
Expand Down
13 changes: 9 additions & 4 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
2024-07-08 -> ['105,106(todo)']
2024-07-07 -> ['242,235']
2024-07-06 -> ['371']
2024-07-05 -> ['121,252']
2024-07-04 -> ['125']
2024-06-17 -> ['105,106(todo)']
2024-06-16 -> ['242,235']
2024-06-15 -> ['371']
2024-06-14 -> ['121,252']
2024-06-13 -> ['125']
2024-06-04 -> ['105,106(todo)']
2024-06-03 -> ['242,235']
2024-06-02 -> ['371']
2024-06-01 -> ['121,252']
2024-05-31 -> ['125']
2024-05-27 -> ['105,106(todo)']
2024-05-26 -> ['242,235']
2024-05-25 -> ['371']
2024-05-24 -> ['121,252']
2024-05-23 -> ['125']
2024-05-22 -> ['105,106(todo)']
2024-05-21 -> ['242,235']
2024-05-20 -> ['371']
2024-05-19 -> ['121,252']
2024-05-19 -> ['105,106(todo)', '121,252']
2024-05-18 -> ['242,235', '125']
2024-05-17 -> ['371']
2024-05-16 -> ['242,235', '121,252']
2024-05-15 -> ['242,235', '371', '125']
2024-05-17 -> ['105,106(todo)', '371']
2024-05-16 -> ['105,106(todo)', '242,235', '121,252']
2024-05-15 -> ['105,106(todo)', '242,235', '371', '125']
2024-05-14 -> ['242,235', '371', '121,252']
2024-05-13 -> ['371', '121,252', '125']
2024-05-12 -> ['121,252', '125']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,62 @@
public class ConstructBinaryTreeFromPreorderAndInorderTraversal {

// V0
// TODO : implement
public TreeNode buildTree(int[] preorder, int[] inorder) {

if (preorder.length == 0) {
return null;
}

if (preorder.length == 1) {
return new TreeNode(preorder[0]);
}

// setup root
TreeNode root = new TreeNode(preorder[0]);
// get root idx
int root_idx = 0;
for (int i = 0; i < inorder.length; i++) {
if (inorder[i] == root.val) {
root_idx = i;
break;
}
}
/** NOTE !!! copy sub array syntax : Arrays.copyOfRange */
/**
* NOTE !!!
*
* 1) we get root_idx from "inorder" to split tree
* 2) root_idx will be used as "length" (root - sub left tree)
* 3) root_idx will be used on both "preorder" and "inorder"
*
* 4) for "preorder",
*
* left sub tree : Arrays.copyOfRange(preorder, 1, root_idx + 1)
* -> left sub tree start from idx = 1
* -> left sub tree end at root_idx + 1 (root_idx is "length" of sub tree)
*
* right sub tree: Arrays.copyOfRange(preorder, 1 + root_idx, preorder.length)
* -> right sub tree start from 1 + root_idx (root_idx is "length" of sub tree)
* -> right sub tree end at end of rest of preorder
*
*/
root.left = buildTree(
Arrays.copyOfRange(preorder, 1, root_idx + 1),
Arrays.copyOfRange(inorder, 0, root_idx)
);

root.right = buildTree(
Arrays.copyOfRange(preorder, root_idx + 1, preorder.length),
Arrays.copyOfRange(inorder, root_idx + 1, inorder.length)
);

return root;
}

// V0'
// IDEA : RECURSION (DFS) (transform below py code to java by GPT)
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Recursion/construct-binary-tree-from-preorder-and-inorder-traversal.py#L36
public TreeNode buildTree(int[] preorder, int[] inorder) {
public TreeNode buildTree_0(int[] preorder, int[] inorder) {
if (preorder.length == 0) {
return null;
}
Expand All @@ -29,8 +79,8 @@ public TreeNode buildTree(int[] preorder, int[] inorder) {
break;
}
}
root.left = buildTree(Arrays.copyOfRange(preorder, 1, index + 1), Arrays.copyOfRange(inorder, 0, index));
root.right = buildTree(Arrays.copyOfRange(preorder, index + 1, preorder.length), Arrays.copyOfRange(inorder, index + 1, inorder.length));
root.left = buildTree_0(Arrays.copyOfRange(preorder, 1, index + 1), Arrays.copyOfRange(inorder, 0, index));
root.right = buildTree_0(Arrays.copyOfRange(preorder, index + 1, preorder.length), Arrays.copyOfRange(inorder, index + 1, inorder.length));
return root;
}

Expand Down
52 changes: 52 additions & 0 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,58 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
return root;
}

// LC 105
// DFS
public TreeNode buildTree(int[] preorder, int[] inorder) {

if (preorder == null && inorder == null){
return null;
}

if (preorder.length == 1 && inorder.length == 1){
return new TreeNode(preorder[0]);
}

TreeNode root = new TreeNode(preorder[0]);

int root_idx = -1;
for (int i = 0; i < inorder.length; i++){
if (inorder[i] == root.val){
root_idx = i;
break;
}
}

root.left = this.buildTree(
Arrays.copyOfRange(preorder, 1,root_idx+1),
Arrays.copyOfRange(inorder, 0,root_idx)
);

root.right = this.buildTree(
Arrays.copyOfRange(preorder, root_idx+1,preorder.length),
Arrays.copyOfRange(inorder, root_idx+1,inorder.length)
);

return root;
}

// public TreeNode help(List<Integer> preorderList, List<Integer> inorderList, TreeNode res){
//
// int root = preorderList.get(0);
//
// int leftIdx = inorderList.get(root) - 1;
//
// int left = inorderList.get(leftIdx);
//
// res = new TreeNode(root);
//
// res.left = new TreeNode(left);
//
// res.right = this.help(Arrays.stream(preorderList, 1,2), inorderList, res);
//
// return res;
// }



}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ def buildTree(self, preorder, inorder):
NOTE !!!
-> # we get index of root.val from "INORDER" to SPLIT TREE
"""
index = inorder.index(root.val) # the index of root at inorder, and we can also get the length of left-sub-tree, right-sub-tree ( preorder[1:index+1]) for following using
index = inorder.index(root.val) # the index of root at inorder, and we can also get the length of left sub-tree, right sub-tree ( preorder[1:index+1]) for following using
# recursion for root.left
#### NOTE : the idx is from "INORDER"
#### NOTE : WE idx from inorder in preorder as well
#### NOTE : the idx will be used as "length"
#### NOTE : WE use idx from inorder in preorder as well
#### NOTE : preorder[1 : index + 1] (for left sub tree)
#### NOTE : preorder[index+1 : :] (for right sub tree)
root.left = self.buildTree(preorder[1 : index + 1], inorder[ : index]) ### since the BST is symmery so the length of left-sub-tree is same in both Preorder and Inorder, so we can use the index to get the left-sub-tree of Preorder as well
# recursion for root.right
root.right = self.buildTree(preorder[index + 1 : ], inorder[index + 1 :]) ### since the BST is symmery so the length of left-sub-tree is same in both Preorder and Inorder, so we can use the index to get the right-sub-tree of Preorder as well
Expand Down

0 comments on commit 12283d4

Please sign in to comment.