From aaf01c2510fcc536f1cf4f30c0d80ed286ee259e Mon Sep 17 00:00:00 2001 From: yennanliu Date: Tue, 5 Mar 2024 23:11:38 +0800 Subject: [PATCH] update 105 java, progress --- data/progress.txt | 2 +- data/to_review.txt | 18 ++--- ...ryTreeFromPreorderAndInorderTraversal.java | 69 +++++++------------ 3 files changed, 35 insertions(+), 54 deletions(-) diff --git a/data/progress.txt b/data/progress.txt index ea741a09..fdc925b4 100644 --- a/data/progress.txt +++ b/data/progress.txt @@ -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 diff --git a/data/to_review.txt b/data/to_review.txt index 0ddfde45..b173ff19 100644 --- a/data/to_review.txt +++ b/data/to_review.txt @@ -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'] diff --git a/leetcode_java/src/main/java/LeetCodeJava/Recursion/ConstructBinaryTreeFromPreorderAndInorderTraversal.java b/leetcode_java/src/main/java/LeetCodeJava/Recursion/ConstructBinaryTreeFromPreorderAndInorderTraversal.java index 096cc75c..77adbe4a 100644 --- a/leetcode_java/src/main/java/LeetCodeJava/Recursion/ConstructBinaryTreeFromPreorderAndInorderTraversal.java +++ b/leetcode_java/src/main/java/LeetCodeJava/Recursion/ConstructBinaryTreeFromPreorderAndInorderTraversal.java @@ -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.