LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Solutions in C++
Hello Programmers/Coders, Today we are going to share solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python. At Each Problem with Successful submission with all Test Cases Passed, you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.
In this post, you will find the solution for the Construct Binary Tree from Inorder and Postorder Traversal in C++, Java & Python-LeetCode problem. We are providing the correct and tested solutions to coding problems present on LeetCode. If you are not able to solve any problem, then you can take help from our Blog/website.
Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.
About LeetCode
LeetCode is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews.
LeetCode is for software engineers who are looking to practice technical questions and advance their skills. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.
LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either Easy, Medium, or Hard.
LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:
- Mathematics/Basic Logical Based Questions
- Arrays
- Strings
- Hash Table
- Dynamic Programming
- Stack & Queue
- Trees & Graphs
- Greedy Algorithms
- Breadth-First Search
- Depth-First Search
- Sorting & Searching
- BST (Binary Search Tree)
- Database
- Linked List
- Recursion, etc.
Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.
Link for the Problem – Construct Binary Tree from Inorder and Postorder Traversal– LeetCode Problem
Construct Binary Tree from Inorder and Postorder Traversal– LeetCode Problem
Problem:
Given two integer arrays inorder
and postorder
where inorder
is the inorder traversal of a binary tree and postorder
is the postorder traversal of the same tree, construct and return the binary tree.
Example 1:
![Construct Binary Tree from Inorder and Postorder Traversal LeetCode Programming Solutions | LeetCode Problem Solutions in C++, Java, & Python [💯Correct] 2 tree](https://assets.leetcode.com/uploads/2021/02/19/tree.jpg)
Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] Output: [3,9,20,null,null,15,7]
Example 2:
Input: inorder = [-1], postorder = [-1] Output: [-1]
Constraints:
1 <= inorder.length <= 3000
postorder.length == inorder.length
-3000 <= inorder[i], postorder[i] <= 3000
inorder
andpostorder
consist of unique values.- Each value of
postorder
also appears ininorder
. inorder
is guaranteed to be the inorder traversal of the tree.postorder
is guaranteed to be the postorder traversal of the tree.
Construct Binary Tree from Inorder and Postorder Traversal– LeetCode Solutions
Construct Binary Tree from Inorder and Postorder Traversal Solution in C++:
class Solution { public: TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { unordered_map<int, int> inToIndex; for (int i = 0; i < inorder.size(); ++i) inToIndex[inorder[i]] = i; return build(inorder, 0, inorder.size() - 1, postorder, 0, postorder.size() - 1, inToIndex); } private: TreeNode* build(const vector<int>& inorder, int inStart, int inEnd, const vector<int>& postorder, int postStart, int postEnd, const unordered_map<int, int>& inToIndex) { if (inStart > inEnd) return nullptr; const int rootVal = postorder[postEnd]; const int rootInIndex = inToIndex.at(rootVal); const int leftSize = rootInIndex - inStart; TreeNode* root = new TreeNode(rootVal); root->left = build(inorder, inStart, rootInIndex - 1, postorder, postStart, postStart + leftSize - 1, inToIndex); root->right = build(inorder, rootInIndex + 1, inEnd, postorder, postStart + leftSize, postEnd - 1, inToIndex); return root; } };
Construct Binary Tree from Inorder and Postorder Traversal Solution in Java:
class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { Map<Integer, Integer> inToIndex = new HashMap<>(); for (int i = 0; i < inorder.length; ++i) inToIndex.put(inorder[i], i); return build(inorder, 0, inorder.length - 1, postorder, 0, postorder.length - 1, inToIndex); } TreeNode build(int[] inorder, int inStart, int inEnd, int[] postorder, int postStart, int postEnd, Map<Integer, Integer> inToIndex) { if (inStart > inEnd) return null; final int rootVal = postorder[postEnd]; final int rootInIndex = inToIndex.get(rootVal); final int leftSize = rootInIndex - inStart; TreeNode root = new TreeNode(rootVal); root.left = build(inorder, inStart, rootInIndex - 1, postorder, postStart, postStart + leftSize - 1, inToIndex); root.right = build(inorder, rootInIndex + 1, inEnd, postorder, postStart + leftSize, postEnd - 1, inToIndex); return root; } }
Construct Binary Tree from Inorder and Postorder Traversal Solution in Python:
class Solution: def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: inToIndex = {num: i for i, num in enumerate(inorder)} def build(inStart: int, inEnd: int, postStart: int, postEnd: int) -> Optional[TreeNode]: if inStart > inEnd: return None rootVal = postorder[postEnd] rootInIndex = inToIndex[rootVal] leftSize = rootInIndex - inStart root = TreeNode(rootVal) root.left = build(inStart, rootInIndex - 1, postStart, postStart + leftSize - 1) root.right = build(rootInIndex + 1, inEnd, postStart + leftSize, postEnd - 1) return root return build(0, len(inorder) - 1, 0, len(postorder) - 1)
Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me?
Thank you for great content, look forward to the continuation. dubai tv live online free
Aluminium recycling resourcefulness Scrap aluminium price analysis Aluminium can recycling process
Very well presented.very quote was awesome and thanks for sharing the content.<a href="https://www.google.co.uk/url?sa=t
Metal scrap repurposing Scrap aluminium recovery technology Segregation of aluminum scrap
Scrap metal recovery solutions, Aluminum cable regulations, Scrap metal exporters
Very nice blog post. definitely love this site.-vox tv programm
Metal recoveryservices Scrap metal reclamation and reuse Iron scrap reclamation plant
Ferrous material user experience, Iron material repurposing, Scrap metal testing
I appreciate you sharing this blog post.hanks Again.Chef Toby Scoochie Pet Products 20R 6 in. Esophagus Jerky One oz Bag in a Zip Lock Bag – Hot Deals
Metal recovery industry Ferrous material recycling green initiatives Iron recycling and reclaiming
Ferrous waste safety standards, Iron scrap reclaiming, Scrap metal investment opportunities
Metal waste storage services Ferrous material recycling licensing Iron scrap removal
Ferrous material health standards, Iron waste reclaiming and repurposing, Scrap metal baling
Good post! We will be linking to this particularly great post on our site. – hey dudes men
Scrap metal material recovery Ferrous metal hauling Iron waste reclaiming and recycling solutions
Ferrous scrap recycling equipment, Iron and steel recycling and reclamation yard, Scrap metal collection services
o great to find someone with some original thoughts on this topic.
The insights you shared are invaluable, thank you! Get More Information
AGENCANTIK
AGENCANTIK says Thank you very much, all the information above is very good and interesting
AGENCANTIK
AGENCANTIK that’s very good
AGENCANTIK
AGENCANTIK says I used to be checking constantly your website.
AGENCANTIK8
AGENCANTIK8 says This is sheer magic for you
Your point of view caught my eye and was very interesting. Thanks. I have a question for you.
I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.