Construct BT from Postorder and Inorder
Construct Binary Tree from Inorder and Postorder Traversal is LeetCode 106 (Medium). Given the inorder and postorder traversals of one binary tree, return the root of the tree they describe.
inordervisits the left subtree first, the node in the middle, and the right subtree last.postordervisits both subtrees, left before right, and the node last.- All values are distinct, so every value identifies one node.
Up to 3,000 values are given, and the target is an O(n) build.
- 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.
Intuition
Postorder writes a node after both of its subtrees, so the last value is the root of the whole tree. Read the array from the back and the order becomes: a root, then its entire right subtree, then its entire left subtree.
Inorder supplies the split: values left of the root form its left subtree, values right of it its right subtree.
So read postorder backwards with one shared index. Each value read is the root of the inorder stretch the current call owns. Since the right subtree comes next in that backwards order, each call builds its right child before its left child; that way the next value read always lands on the side it belongs to.
If a traversal writes each node after its children, read it from the end: roots come out first, and the problem turns into the root-first version with left and right swapped.
Approach
Before reading on, take inorder [4, 2, 5, 1, 3, 6] and postorder [4, 5, 2, 6, 3, 1]. Read postorder from the right, and for each value say which stretch of inorder it splits. Which subtree is finished first?
Map each value to its inorder position
Store pos[value] = index in inorder before building anything. Splitting a stretch around its root then costs one lookup instead of a scan through the array.
Point the shared index at the end of postorder
Set p = n - 1. The last value in postorder is the root of the whole tree, and each step of p to the left reaches the next root to place.
Build the stretch lo..hi
If lo > hi, the stretch is empty, so return nothing. Otherwise make a node from postorder[p], move p one step left, and look up the node's position mid in inorder.
Right child first, then left child
Build the right child from positions mid + 1 to hi first, and the left child from lo to mid - 1 after it. Read backwards, postorder lists a node's right subtree straight after the node, so those are the values p reaches next.
Return the root
build(0, n - 1) creates the node for the last postorder value and, through the calls it makes, every node below it. Return that node.
Code in Python, C++ and Java
p starts at the back and moves left. The whole inorder row (outlined) is the stretch the first call must build. Each node will be drawn straight above its own inorder position.p alone: 2 belongs to the left side or an ancestor.p alone: 2 belongs to an ancestor.p alone: 2 belongs to an ancestor.p alone: 4 belongs to the left side or an ancestor.p alone: 4 belongs to an ancestor.p alone.p alone.p starts at the back and moves left. The whole inorder row (outlined) is the stretch the first call must build. Each node will be drawn straight above its own inorder position.p alone.p alone.p alone.p alone.p starts at the back and moves left. The whole inorder row (outlined) is the stretch the first call must build. Each node will be drawn straight above its own inorder position.p alone.p alone.Common pitfalls
Building the left child first
node.left = build(lo, mid - 1) node.right = build(mid + 1, hi)
node.right = build(mid + 1, hi) node.left = build(lo, mid - 1)
The value p reaches next is the root of the right subtree, and the left call takes it. On inorder [4, 2, 5, 1, 3, 6] and postorder [4, 5, 2, 6, 3, 1], the left side of 1 should hold 4, 2 and 5, but it is handed 3, which belongs on the right, so the tree comes out wrong.
Reading postorder from the front
p = 0 ... p += 1
p = len(postorder) - 1 ... p -= 1
The front of postorder is the leftmost leaf, not the root. On the same input the first value read is 4, so a leaf is made the root of the whole tree.
Slicing the right part of postorder one place too far
node.right = self.buildTree(inorder[mid + 1:], postorder[mid + 1:])
node.right = self.buildTree(inorder[mid + 1:], postorder[mid:-1])
In postorder the right subtree starts straight after the mid left-subtree values, at index mid, and stops before the root at the end. On the input above mid is 3: the right part should be [6, 3], but postorder[4:] gives [3, 1], which drops 6 and brings the root 1 back in.
Edge cases
Inorder is then postorder reversed, such as [1, 2, 3] and [3, 2, 1]. Every left call returns at once, while the right calls nest up to 3,000 deep. Python's default recursion limit is 1,000, so a runner that keeps it needs sys.setrecursionlimit; C++ and Java cope.
Complexity
Which traversal pair is this?
Both inorder pairs take the split from inorder; what changes is where the roots come from and which side is built first. 889 has no inorder at all.
| Problem | Where the next root is | Read order | Children built |
|---|---|---|---|
| 105 Preorder + Inorder | front of preorder | front to back | left, then right |
| 106 Inorder + Postorder (this page) | back of postorder | back to front | right, then left |
| 889 Preorder + Postorder | front of preorder | front to back | left, then right; unique only if every node has 0 or 2 children |
Construct BT from Postorder and Inorder FAQ
Can I get the preorder traversal without building the tree?
Yes. Give each call a range in both arrays. The root is the last value of its postorder range, and its inorder position gives the left size k: the first k values of the postorder range are the left subtree, the rest before the root are the right. Output the root, then recurse on the left ranges, then the right.
Is there a version without recursion?
Yes. Walk postorder from the back with a stack of nodes that may still get a left child, and a second pointer walking inorder from the back. Each new value is the right child of the top node, unless the top matches the inorder pointer: then pop while they match, and the new value is the left child of the last node popped. It is O(n) too, and avoids deep recursion on a chain.