LeetCode Solutions
106. Construct Binary Tree from Inorder and Postorder Traversal
Time: $O(n)$ Space: $O(n)$
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;
}
};
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;
}
}
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)