FastPrepConstruct a Tree from Level-Order and Inorder Traversals
Problems/Construct a Tree from Level-Order and Inorder Traversals

Construct a Tree from Level-Order and Inorder Traversals

Amazon logoAmazon● HardFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source asks to construct a tree from level-order and inorder traversals but does not specify value uniqueness, the returned tree convention, or input bounds. The judged task uses distinct values, valid traversals of the same binary tree, a conventional binary-tree root result, and at most 1,200 nodes; with those explicit conventions, it matches the source at about 91%.

Given the levelOrder and inorder traversals of the same binary tree, reconstruct and return its root.

All node values are distinct. Both arrays contain the same values, and together they describe exactly one valid binary tree.

Function

buildTree(levelOrder: int[], inorder: int[]) → TreeNode

Examples

Example 1

levelOrder = [3,9,20,15,7]inorder = [9,3,15,20,7]return = [3,9,20,null,null,15,7]

The root 3 appears first in level order; the inorder split places 9 left and the remaining nodes right.

Example 2

levelOrder = [1]inorder = [1]return = [1]

Both traversals describe a one-node tree.

Example 3

levelOrder = [1,2,3,4,5]inorder = [4,2,5,1,3]return = [1,2,3,4,5]

The traversals reconstruct the shown complete upper levels.

Constraints

  • 1 <= levelOrder.length == inorder.length <= 1200.
  • -10^9 <= levelOrder[i], inorder[i] <= 10^9.
  • Each traversal contains distinct values, and both contain the same set of values.
  • The arrays are valid traversals of one binary tree.

More Amazon problems

  • Count Adjacent Symbol Pairs for BPEONSITE INTERVIEW · Seen Nov 2026
  • First Non-Repeating CharacterOA · ONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
See Amazon hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] levelOrder, int[] inorder) {
      // write your code here
    }
}
Checking account…