Construct Binary Tree From Inorder And Postorder Traversal
To solve this coding challenge of reconstructing a binary tree from its inorder and postorder traversals, we need to understand how both traversal types work:
- Inorder Traversal (Left, Root, Right): This means for any node, we visit all nodes in its left subtree first, then the node itself, and finally all nodes in its right subtree.
- Postorder Traversal (Left, Right, Root): This means for any node, we visit all nodes in its left subtree first, then all nodes in its right subtree, and finally the node itself. The key point to solving this challenge is to recognize that in postorder traversal, the last element is the root of the binary tree.
- Identify the Root : The last element in the postorder array is the root of the tree.
- Find the Root in Inorder Array : Using this root value, locate its index in the inorder array. Elements to the left in the inorder array are part of the left subtree, and elements to the right are part of the right subtree.
- Recursive Construction : Recursively apply the process to the left and right subtrees, using the corresponding slices of the inorder and postorder arrays.
-
Base Case
: If the range for the inorder or postorder array is invalid (start index greater than end index), return
null
- Identify the Root :
- The root value is the last element in the current range of the postorder array.
- Create a new tree node with this root value.
- Find Split Point :
- Locate the root value's index in the inorder array. This will determine the boundaries of the left and right subarrays.
- The number of elements in the left subtree can be calculated as the difference between the root index in the inorder array and the starting index of the current inorder range.
- Recursive Calls :
- Recursively build the left subtree with the respective ranges in the inorder and postorder arrays.
- Recursively build the right subtree with the respective ranges in the inorder and postorder arrays.
- Combine : Assign the recursively built left and right subtrees to the root node.
Explanation
Detailed Steps in Pseudocode
# Pseudocode for constructing the binary tree from inorder and postorder traversals
# Function to build tree
FUNCTION buildTree(inorder, postorder):
# Helper function to construct the tree
FUNCTION build(inStart, inEnd, postStart, postEnd):
# Base case: if no elements to consider in the current segment, return null
IF inStart > inEnd OR postStart > postEnd:
RETURN null
# The root value is the last element in the current segment of the postorder array
rootValue = postorder[postEnd]
# Create a new tree node with the root value
root = new TreeNode(rootValue)
# Find the index of root value in the inorder array
inIndex = FIND_INDEX(inorder, rootValue)
# Determine the size of the left subtree
leftSize = inIndex - inStart
# Recursively build the left subtree
root.left = build(inStart, inIndex - 1, postStart, postStart + leftSize - 1)
# Recursively build the right subtree
root.right = build(inIndex + 1, inEnd, postStart + leftSize, postEnd - 1)
# Return the constructed tree node
RETURN root
# Call the helper function with the entire range of both arrays
RETURN build(0, LENGTH(inorder) - 1, 0, LENGTH(postorder) - 1)
In this pseudocode:
- The base case handles the scenario when an invalid range is encountered, ensuring the recursion terminates correctly.
-
The
FIND_INDEX
-
The calculation of
leftSize