Skip to content

Commit

Permalink
update 105 java, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Mar 5, 2024
1 parent bf45a6f commit aaf01c2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 54 deletions.
2 changes: 1 addition & 1 deletion data/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
20240305: 100,102,104
20240305: 100,102,104,105(again)
20240304: 73,76,79(again),91,25
20240303: 55(again),56,62,70
20240229: 39,48(again),49,53,54
Expand Down
18 changes: 9 additions & 9 deletions data/to_review.txt
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
2024-04-29 -> ['100,102,104']
2024-04-29 -> ['100,102,104,105(again)']
2024-04-28 -> ['73,76,79(again),91,25']
2024-04-27 -> ['55(again),56,62,70']
2024-04-24 -> ['39,48(again),49,53,54']
2024-04-23 -> ['20,21,23,33(again)']
2024-04-22 -> ['1,3,5,4,19']
2024-04-08 -> ['100,102,104']
2024-04-08 -> ['100,102,104,105(again)']
2024-04-07 -> ['73,76,79(again),91,25']
2024-04-06 -> ['55(again),56,62,70']
2024-04-03 -> ['39,48(again),49,53,54']
2024-04-02 -> ['20,21,23,33(again)']
2024-04-01 -> ['1,3,5,4,19']
2024-03-26 -> ['100,102,104']
2024-03-26 -> ['100,102,104,105(again)']
2024-03-25 -> ['73,76,79(again),91,25']
2024-03-24 -> ['55(again),56,62,70']
2024-03-21 -> ['39,48(again),49,53,54']
2024-03-20 -> ['20,21,23,33(again)']
2024-03-19 -> ['1,3,5,4,19']
2024-03-18 -> ['100,102,104']
2024-03-18 -> ['100,102,104,105(again)']
2024-03-17 -> ['73,76,79(again),91,25']
2024-03-16 -> ['55(again),56,62,70']
2024-03-13 -> ['100,102,104', '39,48(again),49,53,54']
2024-03-13 -> ['100,102,104,105(again)', '39,48(again),49,53,54']
2024-03-12 -> ['73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-11 -> ['55(again),56,62,70', '1,3,5,4,19']
2024-03-10 -> ['100,102,104']
2024-03-10 -> ['100,102,104,105(again)']
2024-03-09 -> ['73,76,79(again),91,25']
2024-03-08 -> ['100,102,104', '55(again),56,62,70', '39,48(again),49,53,54']
2024-03-07 -> ['100,102,104', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-06 -> ['100,102,104', '73,76,79(again),91,25', '55(again),56,62,70', '1,3,5,4,19']
2024-03-08 -> ['100,102,104,105(again)', '55(again),56,62,70', '39,48(again),49,53,54']
2024-03-07 -> ['100,102,104,105(again)', '73,76,79(again),91,25', '20,21,23,33(again)']
2024-03-06 -> ['100,102,104,105(again)', '73,76,79(again),91,25', '55(again),56,62,70', '1,3,5,4,19']
2024-03-05 -> ['73,76,79(again),91,25', '55(again),56,62,70', '39,48(again),49,53,54']
2024-03-04 -> ['55(again),56,62,70', '20,21,23,33(again)']
2024-03-03 -> ['39,48(again),49,53,54', '1,3,5,4,19']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,31 @@

public class ConstructBinaryTreeFromPreorderAndInorderTraversal {

// // V0
// // IDEA : DFS
// TreeNode root = new TreeNode();
// 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]);
// }
//
// // help func
// //this._buildTree(preorder, inorder);
// return this.root;
// }


// private TreeNode _buildTree(int[] preOrder, int[] inOrder){
//
// //Arrays.copyOfRange(preOrder, 2, 3);
// //if (preOrder == null)
// if (preOrder.equals(inOrder)){
// return null;
// }
//
// if (preOrder.equals(null) || inOrder.equals(null)){
// return null;
// }
// int _root = preOrder[0];
// int _left = inOrder[0];
// int idxRightPreorder = Arrays.asList(preOrder).indexOf(_left)+1;
// int idxRightInorder = Arrays.asList(inOrder).indexOf(_root)+1;
//
// this.resNode = new TreeNode(_root);
// this.resNode.left = new TreeNode(_left);
// this.resNode.left = this._buildTree(
// Arrays.copyOfRange(preOrder, idxRightPreorder, preOrder.length-1),
// Arrays.copyOfRange(inOrder, idxRightInorder, inOrder.length-1)
// );
//
// return null;
// }
// V0
// TODO : implement

// 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) {
if (preorder.length == 0) {
return null;
}
if (preorder.length == 1) {
return new TreeNode(preorder[0]);
}
TreeNode root = new TreeNode(preorder[0]);
int index = 0;
for (int i = 0; i < inorder.length; i++) {
if (inorder[i] == root.val) {
index = i;
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));
return root;
}

// V1
// IDEA : The recursive structure in a Tree.
Expand Down

0 comments on commit aaf01c2

Please sign in to comment.