You are 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 = [2,1,3,4], postorder =
Output: [1,2,3,null,null,null,4]Example 2:
Input: inorder = [1], postorder = [1]
Output: [1]Constraints:
1 <= postorder.length == inorder.length <= 3000.-3000 <= inorder[i], postorder[i] <= 3000inorder and postorder consist of unique values.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.The last element of postorder is always the root. Once we identify the root, we find it in the inorder array. Everything to the left of the root in inorder belongs to the left subtree, and everything to the right belongs to the right subtree. We recursively apply this logic, slicing the arrays accordingly.
postorder or inorder is empty, return null.postorder.mid of the root value in inorder.inorder[0:mid] and postorder[0:mid].inorder[mid+1:] and postorder[mid:-1].# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
if not postorder or not inorder:
return None
root = TreeNode(postorder[-1])
mid = inorder.index(postorder[-1])
root.left = self.buildTree(inorder[:mid], postorder[:mid])
root.right = self.buildTree(inorder[mid + 1:], postorder[mid:-1])
return rootThe previous approach has O(n) lookup time when finding the root in inorder. By precomputing a hash map that stores each value's index in inorder, we achieve O(1) lookups. We also avoid creating new arrays by using index boundaries instead.
inorder to its index.postIdx starting at the end of postorder.dfs(l, r) that builds the tree for the inorder range [l, r]:l > r, return null.postorder (decrement postIdx), create a node.inorder using the hash map.postorder from the end), then the left subtree.dfs(0, n-1).# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
inorderIdx = {v: i for i, v in enumerate(inorder)}
def dfs(l, r):
if l > r:
return None
root = TreeNode(postorder.pop())
idx = inorderIdx[root.val]
root.right = dfs(idx + 1, r)
root.left = dfs(l, idx - 1)
return root
return dfs(0, len(inorder) - 1)We can eliminate the hash map by using a boundary-based approach. Instead of explicitly finding the root's position, we pass a "limit" value that tells us when to stop building the current subtree. When we encounter the limit in inorder, we know the current subtree is complete. This approach processes both arrays from right to left.
postIdx and inIdx both at the last index.dfs(limit) that builds the subtree bounded by limit:postIdx < 0, return null.inorder[inIdx] equals limit, decrement inIdx and return null (subtree boundary reached).postorder[postIdx], decrement postIdx.dfs with an impossible limit value (like infinity) and return the result.# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
postIdx = inIdx = len(postorder) - 1
def dfs(limit):
nonlocal postIdx, inIdx
if postIdx < 0:
return None
if inorder[inIdx] == limit:
inIdx -= 1
return None
root = TreeNode(postorder[postIdx])
postIdx -= 1
root.right = dfs(root.val)
root.left = dfs(limit)
return root
return dfs(float('inf'))When processing postorder from right to left, you must build the right subtree before the left subtree. The postorder array is structured as [left, right, root], so when traversing backwards, we encounter right subtree elements first.
# Wrong: Building left before right
root.left = dfs(l, idx - 1)
root.right = dfs(idx + 1, r) # postIdx is now wrong
# Correct: Build right first
root.right = dfs(idx + 1, r)
root.left = dfs(l, idx - 1)When slicing arrays for recursive calls, it's easy to miscalculate the postorder boundaries. The left subtree has mid elements, so postorder[:mid] is correct for the left subtree, and postorder[mid:-1] (excluding the root) is correct for the right subtree.
# Wrong: Including the root in right subtree
root.right = self.buildTree(inorder[mid + 1:], postorder[mid:])
# Correct: Exclude the root (last element)
root.right = self.buildTree(inorder[mid + 1:], postorder[mid:-1])In preorder traversal, the root is at the beginning of the array, but in postorder, the root is at the end. Using postorder[0] instead of postorder[-1] as the root will produce an incorrect tree.