Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal arrays of a binary tree, construct the original tree and return its root as a level-order array. Pattern focus: Construct Tree from Traversals. The postorder array identifies the root, and inorder splits the tree into left and right parts.

Input Format

inorder = inorder traversal, postorder = postorder traversal

Output Format

level-order array representation of the reconstructed tree

Constraints

  • 1 <= number of nodes <= 10^5
  • -10^4 <= node values <= 10^4
  • Input must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

inorder = [9,3,15,20,7]
postorder = [9,15,7,20,3]

Output:

[3,9,20,null,null,15,7]

Explanation:

This is the classic reconstruction example.

Example 2:

Input:

inorder = [2,1]
postorder = [2,1]

Output:

[1,2]

Explanation:

The last postorder value is the root.

Loading...
Construct Binary Tree from Inorder and…