Skip to content

Commit

Permalink
update 106 java, py, progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed May 14, 2024
1 parent 12283d4 commit 9fb61ef
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,48 @@

public class ConstructBinaryTreeFromInorderAndPostorderTraversal {

// TODO : fix V0
// V0
// IDEA :
//TreeNode root = new TreeNode();
// public TreeNode buildTree(int[] inorder, int[] postorder) {
//
// if (inorder.length == 1){
// IDEA : DFS
// https://github.com/yennanliu/CS_basics/blob/master/leetcode_python/Recursion/construct-binary-tree-from-inorder-and-postorder-traversal.py
public TreeNode buildTree(int[] inorder, int[] postorder) {

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

// either each of below works
if (postorder.length == 1) {
return new TreeNode(postorder[0]);
}

// if (inorder.length == 1) {
// return new TreeNode(inorder[0]);
// }
//
// //TreeNode _root = new TreeNode(inorder[0]);
//
// // dfs
// return _help(inorder, postorder, 0);
// }
//
// private TreeNode _help(int[] inorder, int[] postorder, int idx){
//
// if (inorder.length == 0){
// return null;
// }
//
// int root_val = postorder[postorder.length-1];
// TreeNode _root = new TreeNode(root_val);
// int root_idx = getIdx(inorder, root_val);
// int left_val = inorder[root_idx-1];
// int left_idx = getIdx(postorder, left_val);
//
// _root.left = _help(Arrays.copyOfRange(inorder, 0, root_idx), Arrays.copyOfRange(postorder, 0, left_idx), root_idx);
// _root.right = _help(Arrays.copyOfRange(inorder, root_idx+1, inorder.length-1), Arrays.copyOfRange(postorder, left_idx+1, root_idx), root_idx);
// return _root;
// }
//
// private int getIdx(int[] input, int val){
// int idx = -1;
// for (int i = 0; i < input.length; i++){
// if(input[i] == val){
// return i;
// }
// }
// return idx;
// }

// NOTE !!! : get root from postorder
TreeNode root = new TreeNode(postorder[postorder.length - 1]);
int root_idx = -1;
for (int i = 0; i < inorder.length; i++) {

/** NOTE !!! need to get root_idx from inorder */
if (inorder[i] == postorder[postorder.length - 1]) {
root_idx = i;
break;
}

}

root.left = this.buildTree(
Arrays.copyOfRange(inorder, 0, root_idx),
Arrays.copyOfRange(postorder, 0, root_idx)
);
root.right = this.buildTree(
Arrays.copyOfRange(inorder, root_idx+1, inorder.length),
Arrays.copyOfRange(postorder, root_idx, postorder.length-1)
);

return root;
}

// V1
// IDEA : RECURSION
Expand Down
102 changes: 71 additions & 31 deletions leetcode_java/src/main/java/dev/workspace3.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,56 +241,96 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {

// LC 105
// DFS
public TreeNode buildTree(int[] preorder, int[] inorder) {
// 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;
// }

// LC 106
// inorder : left -> root -> right
// postorder : left -> right -> root
public TreeNode buildTree(int[] inorder, int[] postorder) {

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

if (preorder.length == 1 && inorder.length == 1){
return new TreeNode(preorder[0]);
// either each of below works
if (postorder.length == 1) {
return new TreeNode(postorder[0]);
}

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

TreeNode root = new TreeNode(postorder[postorder.length - 1]);
int root_idx = -1;
for (int i = 0; i < inorder.length; i++){
if (inorder[i] == root.val){
for (int i = 0; i < inorder.length; i++) {
/** NOTE !!! need to get root_idx from inorder */
if (inorder[i] == postorder[postorder.length - 1]) {
root_idx = i;
break;
}
}

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

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

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 @@ -30,6 +30,19 @@
"""

# V0
class Solution(object):
def buildTree(self, inorder, postorder):
if len(postorder) == 0:
return None
if len(inorder) == 1:
return TreeNode(inorder[0])
root = TreeNode(postorder[-1]) # get the value of "root" via postorder
index = inorder.index(postorder[-1]) # get the index of "root" in inorder
root.left = self.buildTree(inorder[ : index ], postorder[ : index ])
root.right = self.buildTree(inorder[ index + 1 : ], postorder[ index : - 1 ])
return root

# V0
# IDEA : Binary Tree property, same as LC 105
class Solution(object):
Expand All @@ -51,20 +64,7 @@ def buildTree(self, inorder, postorder):
### NOTE : postorder[idx:-1]
root.right = self.buildTree(inorder[idx+1:], postorder[idx:-1])
return root

# V0'
class Solution(object):
def buildTree(self, inorder, postorder):
if len(postorder) == 0:
return None
if len(inorder) == 1:
return TreeNode(inorder[0])
root = TreeNode(postorder[-1]) # get the value of "root" via postorder
index = inorder.index(postorder[-1]) # get the index of "root" in inorder
root.left = self.buildTree(inorder[ : index ], postorder[ : index ])
root.right = self.buildTree(inorder[ index + 1 : ], postorder[ index : - 1 ])
return root


# V0'
class Solution(object):
def buildTree(self, inorder, postorder):
Expand Down

0 comments on commit 9fb61ef

Please sign in to comment.