Skip to content

Done trees-2#1581

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

Done trees-2#1581
sainathek1999 wants to merge 1 commit into
super30admin:masterfrom
sainathek1999:master

Conversation

@sainathek1999
Copy link
Copy Markdown

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

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

Your solution is on the right track, but there are a few issues to address:

  1. Comment Correction: You have a comment that says "Root from preorder" in the helper method. This is incorrect because we are using postorder traversal. Please correct this to "Root from postorder" to avoid confusion.

  2. Variable Naming: The global variable idx is appropriately named, but ensure that it is clear that it tracks the current position in the postorder array. Consider renaming it to postIndex or similar for clarity.

  3. Algorithm Understanding: You correctly process the right subtree before the left subtree because postorder traversal is left, right, root and we are traversing backwards. This is correct. However, make sure you understand why: in postorder, the root is at the end, and the elements just before the root are the right subtree's root, then left, etc. So when building from the end, we build right first, then left.

  4. Edge Cases: Your code handles the base case (start > end) correctly, which is important for terminating recursion.

  5. Code Structure: The code is well-organized, with a helper function and a HashMap for efficient lookups. This is good practice.

To improve, correct the comment and ensure all references to traversal orders are accurate. Also, consider adding more comments to explain why you are building the right subtree first.

VERDICT: NEEDS_IMPROVEMENT


Root to Leaf Sum (problem2.java)

Your solution is excellent and matches the reference solution closely. You have correctly implemented the DFS traversal and the logic for building the number. Your comments are clear and demonstrate a good understanding of the complexities.

One small suggestion: In the helper function, after checking for the leaf node, you don't need to return immediately. Instead, you can let the recursion handle the null children. This is because when you call helper(root.left, sum) and root.left is null, the base case will return. So the current return statement after adding the leaf node is not necessary. However, it is also not harmful because the leaf node has no children. Alternatively, you can remove the return statement and the code will still work correctly. For example:

if (root.left == null && root.right == null) {
    result += sum;
    // return;   // This return can be removed
}

But this is a minor point and your solution is correct as is.

Overall, great job! Your code is efficient and clean.

VERDICT: PASS

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