Skip to content

Implement binary tree construction from traversals#537

Open
aanya1407jain wants to merge 1 commit intocodedecks-in:masterfrom
aanya1407jain:patch-1
Open

Implement binary tree construction from traversals#537
aanya1407jain wants to merge 1 commit intocodedecks-in:masterfrom
aanya1407jain:patch-1

Conversation

@aanya1407jain
Copy link
Copy Markdown

@aanya1407jain aanya1407jain commented Apr 6, 2026

Pull Request Template

Description

This PR adds a solution for "Construct Binary Tree from Inorder and Postorder Traversal" (LeetCode #106).

The problem requires constructing a binary tree using given inorder and postorder traversal arrays.

Approach
The last element in the postorder array is always the root of the tree.
Using a HashMap, we store the indices of elements in the inorder array for O(1) lookup.
We recursively construct the tree:
Identify the root from postorder.
Split the inorder array into left and right subtrees.
Recursively build the right subtree first, then the left subtree (since we traverse postorder backwards).

This approach ensures optimal performance.

Complexity
Time Complexity: O(n)
Space Complexity: O(n)
Dependencies
Java HashMap (for optimized lookup)
✅ Put check marks:
Have you made changes in README
file ?
Added problem & solution under correct topic
Specified Space & Time complexity
Specified difficulty level, tag & Note(if any)
How Has This Been Tested?

The solution has been tested using multiple cases:

Test A – Example case

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

Output tree matches expected structure.

Test B – Edge cases
Empty tree
Single node tree
Skewed tree (all left / all right)

✅ Checklist
My code follows the style guidelines of this project
I have performed a self-review of my own code
I have commented my code so that it is easy to understand
I have made corresponding changes to the documentation
My changes generate no new warnings
Any dependent changes have been merged and published in downstream modules

@aanya1407jain
Copy link
Copy Markdown
Author

Hi maintainers,
I have implemented the solution for LeetCode #106.
Kindly review my PR and let me know if any changes are required.

Thank you!

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.

1 participant