# Java iterative solution with explanation

• ``````public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length == 0 || postorder.length == 0) return null;
int ip = inorder.length - 1;
int pp = postorder.length - 1;

Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode prev = null;
TreeNode root = new TreeNode(postorder[pp]);
stack.push(root);
pp--;

while (pp >= 0) {
while (!stack.isEmpty() && stack.peek().val == inorder[ip]) {
prev = stack.pop();
ip--;
}
TreeNode newNode = new TreeNode(postorder[pp]);
if (prev != null) {
prev.left = newNode;
} else if (!stack.isEmpty()) {
TreeNode currTop = stack.peek();
currTop.right = newNode;
}
stack.push(newNode);
prev = null;
pp--;
}

return root;
}
``````

This is my iterative solution, think about "Constructing Binary Tree from inorder and preorder array", the idea is quite similar. Instead of scanning the preorder array from beginning to end and using inorder array as a kind of mark, in this question, the key point is to scanning the postorder array from end to beginning and also use inorder array from end to beginning as a mark because the logic is more clear in this way. The core idea is: Starting from the last element of the postorder and inorder array, we put elements from postorder array to a stack and each one is the right child of the last one until an element in postorder array is equal to the element on the inorder array. Then, we pop as many as elements we can from the stack and decrease the mark in inorder array until the peek() element is not equal to the mark value or the stack is empty. Then, the new element that we are gonna scan from postorder array is the left child of the last element we have popped out from the stack.

• `````` public TreeNode buildTree(int[] inorder, int[] postorder) {
//boundary case
if(inorder.length == 0) return null;

Stack<TreeNode> stack = new Stack<TreeNode>();
TreeNode root = new TreeNode(postorder[postorder.length-1]);
stack.push(root);

int i = postorder.length-2, j = inorder.length-1;//i is index in postorder[], j is index in inorder[]

while(i >= 0){
TreeNode curr = stack.peek();
if(curr.val != inorder[j]){
//as long as we have not reach the rightmost node we can safely follow right path and attach right child
TreeNode right = new TreeNode(postorder[i]);
curr.right = right;
stack.push(right);
i--;
}else{
//found the node from stack where we have not visited its left subtree
while(!stack.isEmpty() && stack.peek().val == inorder[j]){
curr = stack.pop();
j--;
}

TreeNode left = new TreeNode(postorder[i]);
curr.left = left;
stack.push(left);
i--;
}
}

return root;
}
``````

My iterative solution

• Fantasic.....How could you come up with this logic.....

• This post is deleted!

• best solution so far, thanks

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.