Skip to content

Complete Trees-2#1589

Open
dhruvil15 wants to merge 1 commit into
super30admin:masterfrom
dhruvil15:master
Open

Complete Trees-2#1589
dhruvil15 wants to merge 1 commit into
super30admin:masterfrom
dhruvil15:master

Conversation

@dhruvil15
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

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

Strengths:

  • The solution correctly uses a hashmap to store inorder indices, which allows O(1) lookup during tree construction.
  • The recursive helper function is implemented correctly, with base case handling for when the start index exceeds the end index.
  • The code is clean, well-commented, and follows good practices by making the helper function private.

Areas for Improvement:

  • While the solution is correct, it's important to note that the helper function should be called with the correct indices. The student's code uses this.postOrderIdx which is a class variable, but in Java, it's often better to avoid using class variables for recursion to make the code more thread-safe and reusable. However, in this context, since the method is not called concurrently, it is acceptable. Alternatively, the index could be passed as a parameter or managed in an iterative way, but the current approach is standard.
  • The variable names are clear, but consider using more descriptive names for the helper function parameters (e.g., inStart and inEnd instead of start and end) to clarify that they refer to indices in the inorder array.
  • The code does not handle the case where the input arrays are empty, but the constraints state that the length is at least 1, so it's acceptable.

Overall, the solution is excellent and matches the reference solution closely.

VERDICT: PASS


Root to Leaf Sum (Problem46.java)

Your solution is on the right track, but there is a logical error in the placement of the leaf node check. Currently, you are checking if the current node is a leaf only after traversing the left subtree. This means that for a leaf node in the right subtree, the check will never be performed because the recursive call for the right subtree happens after the check. Additionally, for a leaf node in the left subtree, the check is performed after the left recursive call, which is unnecessary since the left recursive call will return immediately for a leaf (because its left and right are null). However, the main issue is that the right leaf nodes are never processed.

To fix this, you should check for a leaf node immediately after updating the current number, before making any recursive calls. This ensures that every leaf node is correctly identified and its value added to the result. Here's the corrected version of your helper method:

private void helper(TreeNode root, int currSum) {
    if (root == null) return;
    
    currSum = currSum * 10 + root.val;
    
    if (root.left == null && root.right == null) {
        result += currSum;
        return; // optional: return early since no children
    }
    
    helper(root.left, currSum);
    helper(root.right, currSum);
}

Alternatively, you can keep the recursive calls without the early return, but the key is to move the leaf check to after updating the current sum and before the recursive calls.

Another point: your code style is good, and you have correctly analyzed the time and space complexity. Keep up the good work in writing clean code!

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