Implement binary tree construction from traversals#537
Open
aanya1407jain wants to merge 1 commit intocodedecks-in:masterfrom
Open
Implement binary tree construction from traversals#537aanya1407jain wants to merge 1 commit intocodedecks-in:masterfrom
aanya1407jain wants to merge 1 commit intocodedecks-in:masterfrom
Conversation
Author
|
Hi maintainers, Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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