304 North Cardinal St.
Dorchester Center, MA 02124

# Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution

## Problem – Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution

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:

``````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` and `postorder` consist of unique values.
• Each value of `postorder` also appears in `inorder`.
• `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 Solution in Python

``````class Solution:
def buildTree(self, inorder, postorder):
if not inorder or not postorder:
return None

root = TreeNode(postorder.pop())
inorderIndex = inorder.index(root.val) # Line A

root.right = self.buildTree(inorder[inorderIndex+1:], postorder) # Line B
root.left = self.buildTree(inorder[:inorderIndex], postorder) # Line C

return root
``````

### Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution in C++

``````class Solution {
public:
TreeNode *Tree(vector<int>& in, int x, int y,vector<int>& po,int a,int b){
if(x > y || a > b)return nullptr;
TreeNode *node = new TreeNode(po[b]);
int SI = x;
while(node->val != in[SI])SI++;
node->left  = Tree(in,x,SI-1,po,a,a+SI-x-1);
node->right = Tree(in,SI+1,y,po,a+SI-x,b-1);
return node;
}
TreeNode* buildTree(vector<int>& in, vector<int>& po){
return Tree(in,0,in.size()-1,po,0,po.size()-1);
}
};
``````

### Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution in Java

``````public TreeNode buildTreePostIn(int[] inorder, int[] postorder) {
if (inorder == null || postorder == null || inorder.length != postorder.length)
return null;
HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>();
for (int i=0;i<inorder.length;++i)
hm.put(inorder[i], i);
return buildTreePostIn(inorder, 0, inorder.length-1, postorder, 0,
postorder.length-1,hm);
}

private TreeNode buildTreePostIn(int[] inorder, int is, int ie, int[] postorder, int ps, int pe,
HashMap<Integer,Integer> hm){
if (ps>pe || is>ie) return null;
TreeNode root = new TreeNode(postorder[pe]);
int ri = hm.get(postorder[pe]);
TreeNode leftchild = buildTreePostIn(inorder, is, ri-1, postorder, ps, ps+ri-is-1, hm);
TreeNode rightchild = buildTreePostIn(inorder,ri+1, ie, postorder, ps+ri-is, pe-1, hm);
root.left = leftchild;
root.right = rightchild;
return root;
}
``````
##### Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution Review:

In our experience, we suggest you solve this Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution

Find on LeetCode

##### Conclusion:

I hope this Construct Binary Tree from Inorder and Postorder Traversal LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions