Skip to content

Completed Trees-2 Assignemt#1587

Open
Hriday-A wants to merge 1 commit into
super30admin:masterfrom
Hriday-A:master
Open

Completed Trees-2 Assignemt#1587
Hriday-A wants to merge 1 commit into
super30admin:masterfrom
Hriday-A:master

Conversation

@Hriday-A
Copy link
Copy Markdown

@Hriday-A Hriday-A commented May 4, 2026

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Construct Binary Tree from Inorder and Postorder Traversal (postorder_inorder_contruct_tree.java)


Now, let's evaluate the student's solution step by step.

First, I'll check the correctness of the student's solution.

The problem is to construct a binary tree from inorder and postorder traversal arrays. The reference solution uses a recursive approach with a helper function. It uses a map to store indices of inorder elements for quick lookup. The key idea is to traverse the postorder array from the end to the beginning (which gives the root nodes) and use the inorder array to determine the left and right subtrees.

Looking at the student's solution:

1. The student uses a similar approach: they have a map to store inorder indices, and they traverse the postorder array backwards (from last to first). However, there is a critical mistake in the student's code.

   In the helper function, the student is recursively building the tree. But note: in postorder traversal, the last element is the root. Then, the elements before that represent first the right subtree and then the left subtree? Actually, in postorder, the order is left, right, root. So when we traverse backwards, we get root, then right subtree, then left subtree. Therefore, we should process the right subtree first and then the left subtree.

   The reference solution does exactly that: it decrements the postOrderIdx and then first builds the right subtree and then the left subtree.

   However, in the student's solution, the helper function is called for the right subtree first? Let's see:

   The student has:

root.right = helper(postorder,rootIdx+1,end,map);
root.left = helper(postorder,st, rootIdx-1,map);


But wait, the student is passing the same postorder array and the same map, but the index `idx` is a class variable that is being decremented in each call? Actually, the student uses `idx` which is initialized to `postorder.length-1` and then decremented in the helper function? However, in the helper function, the student does not decrement `idx` until after they use it? Let me look again.

The student's helper function:

private TreeNode helper(int[] postorder, int st, int end, HashMap<Integer, Integer> map){
if(st>end) return null;
int rootVal = postorder[idx];
idx--;
TreeNode root= new TreeNode(rootVal);
int rootIdx= map.get(rootVal);
root.right = helper(postorder,rootIdx+1,end,map);
root.left = helper(postorder,st, rootIdx-1,map);
return root;
}


There is a problem here: the student decrements `idx` immediately after reading the root value. Then they get the root index from the map. Then they call first for the right subtree and then for the left subtree.

However, when building the tree from postorder, we must traverse the postorder array in reverse order. The order of processing should be: root, then right subtree, then left subtree. So the index should be decremented before building the right subtree? Actually, in the reference solution, the index is decremented in the recursive calls: the helper function uses the global index and decrements it when it returns? Wait, let me compare.

Reference solution in C++:

TreeNode* helper(vector& postorder, int start, int end) {
if(start > end) return nullptr;

    int rootVal = postorder[postOrderIdx];
    int rootIdx = map[rootVal];
    postOrderIdx--;

    TreeNode* node = new TreeNode(rootVal);
    node->right = helper(postorder, rootIdx + 1, end);
    node->left = helper(postorder, start, rootIdx - 1);

    return node;
}

Here, the index `postOrderIdx` is decremented after reading the root value and before building the subtrees. Then, it first builds the right subtree and then the left subtree. This is correct because in postorder, the right subtree comes immediately before the left subtree in the array when traversed backwards? Actually, when we traverse the postorder array backwards, the sequence is: root, then right subtree's root, then right subtree's elements, then left subtree's root, etc. So for a given root, the next element (at index decremented by one) is the root of the right subtree. Therefore, we must build the right subtree first and then the left subtree.

Now, in the student's solution, the order of recursive calls is correct: first right then left. However, there is a critical error: the student passes the same `st` and `end` parameters to both recursive calls? Actually, no: for the right subtree, the student passes `rootIdx+1` to `end`, and for the left subtree, `st` to `rootIdx-1`. This is correct because in the inorder array, the left subtree is from `st` to `rootIdx-1` and the right subtree is from `rootIdx+1` to `end`.

But the issue is with the index `idx`. The student uses a class variable `idx` that is decremented each time a root is processed. However, when we recursively build the right subtree, we use the next available postorder elements for the right subtree, and then the left subtree. This should work.

However, there is a problem: the student's helper function does not check if the current `idx` is within bounds? Actually, the condition `if(st>end) return null;` is correct to avoid processing when there are no elements.

But wait: the student initializes `idx` to `postorder.length-1` and then decrements it in the helper. The number of nodes is exactly the length of the array, so we should decrement exactly once per node. This seems correct.

However, there is a logical error: when we build the tree, we must ensure that the postorder index is decremented in the correct order. The student's code might actually work? Let me test with a small example.

Example: 
inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Steps:

Initially, idx = 4 (last index of postorder).
Call helper(postorder, 0, 4, map)
   rootVal = postorder[4] = 3
   idx becomes 3
   rootIdx = map.get(3) = 1 (since inorder[1]=3)
   Then call for right subtree: helper(postorder, 2, 4, map)  [rootIdx+1=2, end=4]
     In this call: 
       rootVal = postorder[3] = 20
       idx becomes 2
       rootIdx = map.get(20)=3
       Then call for right subtree: helper(postorder, 4,4, map) 
         rootVal = postorder[2] = 7
         idx becomes 1
         rootIdx = map.get(7)=4
         Then call for right subtree: helper(postorder,5,4) -> returns null
         Then call for left subtree: helper(postorder,4,3) -> returns null
         So returns node 7
       Then call for left subtree: helper(postorder,2,2, map)
         rootVal = postorder[1] = 15
         idx becomes 0
         rootIdx = map.get(15)=2
         Then call for right: helper(3,2) -> null
         Then call for left: helper(2,1) -> null
         So returns node 15
     So the node 20 has right=7 and left=15? But that is incorrect because in the inorder, for root 20, the left subtree should be 15 and right should be 7? Actually, wait: the inorder array is [9,3,15,20,7]. So for root 20, the left subtree is [15] and right subtree is [7]. So the tree should have 20 with left=15 and right=7. But in the student's code, they are assigning right first and then left. So the node 20 would have right=7 and left=15, which is correct? Because in the tree structure, it doesn't matter the order of assignment as long as the left and right pointers are correct. So the student's code would create:
      20
     /   \
    15    7
   which is correct.

Then after building the right subtree for root 3, we build the left subtree: helper(postorder,0,0, map)
     rootVal = postorder[0] = 9
     idx becomes -1
     rootIdx = map.get(9)=0
     Then call for right: helper(1,0) -> null
     Then call for left: helper(0,-1) -> null
     So returns node 9

So the overall tree for root 3 would be:
     3
    / \
   9   20
       / \
      15  7

But wait, this is incorrect because the right subtree of 3 should be 20 with left 15 and right 7, but the left subtree of 3 is 9. This is correct? Actually, the expected tree is:
     3
    / \
   9  20
      / \
     15  7

So the student's solution seems to build the tree correctly for this example.

However, there is a problem: the student decrements `idx` before building the subt

VERDICT: NEEDS_IMPROVEMENT

---

### Root to Leaf Sum (sum_root_to_leaf.java)
Strengths:
- The solution is correct and efficient.
- The code is well-structured and easy to read.
- The recursive approach is appropriately implemented.

Areas for Improvement:
- The comment about space complexity is incorrect. The space complexity is O(h) due to the recursion stack, not O(1). Please update the comment to reflect this.
- The global variable `res` is not initialized. In the provided code, `res` is declared but not initialized to 0. This will cause a compilation error because `res` might be used without being initialized. You should initialize `res` to 0 in the `sumNumbers` method before calling the helper function.

VERDICT: NEEDS_IMPROVEMENT

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants