Skip to content

trees2 HW#1569

Open
dewann wants to merge 2 commits into
super30admin:masterfrom
dewann:master
Open

trees2 HW#1569
dewann wants to merge 2 commits into
super30admin:masterfrom
dewann:master

Conversation

@dewann
Copy link
Copy Markdown

@dewann dewann commented Feb 8, 2026

No description provided.

@super30admin
Copy link
Copy Markdown
Owner

Strengths:

  • You have correctly implemented the recursive approach with a hashmap for index lookup, which is efficient.
  • The time and space complexity are optimal.
  • The code is concise and easy to follow.

Areas for Improvement:

  • Variable names: Use more descriptive names. For example, st could be renamed to start for clarity.
  • The initial call to helper uses postorder.length-1 as the end index for the inorder array. This is correct because the inorder array has the same length as postorder. However, it might be better to use inorder.length-1 to make it explicit that we are dealing with the inorder indices. But since both arrays have the same length, it doesn't matter.
  • Consider adding comments to explain the logic, especially why we are building the right subtree first. This can help others understand the code.

Overall, the solution for the first problem is correct and efficient.

Now, regarding the second solution (rootToLeafSum): This was not part of the problem we were evaluating, but if you are submitting multiple solutions, ensure that each is in its proper context. The second solution appears to be for a different problem (Sum Root to Leaf Numbers). It looks correct, but we are not evaluating it here.

@super30admin
Copy link
Copy Markdown
Owner

Strengths:

  • You have correctly implemented the solution using a HashMap to store inorder indices, which allows for efficient root position lookup.
  • The recursive helper function efficiently constructs the tree by processing the postorder array from the end and partitioning the inorder array.
  • The time and space complexity are optimal.

Areas for Improvement:

  • Variable names: Use more descriptive names for variables. For example, st could be renamed to start to improve readability.
  • Comments: Adding comments to explain the purpose of the HashMap and the recursive helper function would make the code more understandable.
  • Error handling: Although the problem constraints guarantee non-empty arrays, it's good practice to check for null or empty inputs. However, in this case, the problem states that the arrays are non-empty, so it's acceptable.

Minor issue:

  • In the helper function, the base condition is correct, but ensure that the indices are handled correctly. Your implementation is correct.

Overall, the solution is correct and efficient.

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