Input: postorder =[2,4,3,8,7,5]Output:
5/\37/\\248Explanation: Given the sequence of postorder traversal [2,4,3,8,7,5], the tree is constructed as shown above.
In a postorder traversal, the last element is always the root of the tree.
All elements to the left of this root in the traversal list are elements of the left subtree, and all elements to the right are elements of the right subtree.
Recursively apply the same logic to construct the left and right subtrees.
Root Identification: The last element of the sequence is the root of the subtree.
Partition: Find the first index in the sequence from left where the elements are greater than the root. Elements before this index form the left subtree, and elements from this index to the end (excluding the last element) form the right subtree.
Recursive Construction: Recursively construct the left and right subtrees using the identified partitions.
Return Root: Create the root node with its left and right children.
publicclassSolution {
public TreeNode buildTree(int[] postorder) {
return helper(postorder, 0, postorder.length- 1);
}
private TreeNode helper(int[] postorder, int left, int right) {
if (left > right) returnnull;
// The last element of the postorder array will be the root. TreeNode root =new TreeNode(postorder[right]);
int i;
// Find the first element greater than the root to be the start of the right subtreefor (i = left; i < right; i++) {
if (postorder[i]> postorder[right]) break;
}
// Recursive reconstruction of left and right subtrees root.left= helper(postorder, left, i - 1);
root.right= helper(postorder, i, right - 1);
return root;
}
publicstaticvoidmain(String[] args) {
Solution solution =new Solution();
int[] postorder = {2, 4, 3, 8, 7, 5};
TreeNode root = solution.buildTree(postorder);
}
}
classSolution:
defbuildTree(self, postorder: List[int]) -> Optional['Solution.TreeNode']:
defhelper(left: int, right: int) -> Optional['Solution.TreeNode']:
if left > right:
returnNone# The last element in the postorder segment is the root root_val = postorder[right]
root = Solution.TreeNode(root_val)
# Find the first element greater than root to separate left and right subtrees split_index = left
while split_index < right and postorder[split_index] <= root_val:
split_index +=1# Recursively build the left and right subtrees root.left = helper(left, split_index -1)
root.right = helper(split_index, right -1)
return root
return helper(0, len(postorder) -1)
# Example usagesol = Solution()
postorder = [2, 4, 3, 8, 7, 5]
root = sol.buildTree(postorder)