FastPrepInorder, Preorder and Postorder Traversals
Problems/Inorder, Preorder and Postorder Traversals

Inorder, Preorder and Postorder Traversals

AMD logoAMD● MediumFULLTIMEONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source explicitly asks for code for inorder, preorder and postorder tree traversals. The output grouping, empty convention, bounds and native tree interface are authored; approximate source match95%.

Return three arrays in this exact order: inorder(left,node,right), preorder(node,left,right), and postorder(left,right,node). Include every node value once per traversal, including duplicates. For an empty tree return [[],[],[]]. Leave the input tree unchanged.

Function

traversals(root: TreeNode) → int[][]

Examples

Example 1

root = [1,null,2,3]return = [[1,3,2],[1,2,3],[3,2,1]]

The only left child is3 under2. Inorder visits1,3,2; preorder1,2,3; postorder3,2,1.

Example 2

root = [2,1,3]return = [[1,2,3],[2,1,3],[1,3,2]]

The root2appears between its children in inorder, first in preorder and last in postorder.

Constraints

  • 0 <= every serialized input tree length <= 400.
  • -1000 <= every non-null input node value <= 1000.
  • Every input is a valid acyclic binary tree serialized in level order with null missing children; [] denotes an empty tree.

More AMD problems

  • Insert After a Linked-List NodeONSITE INTERVIEW · Seen Oct 2026
  • LRU CacheONSITE INTERVIEW · Seen Oct 2026
  • Lowest Common Ancestor of a Binary TreePHONE SCREEN · Seen Oct 2026
All AMD interview questionsSee AMD hiring insights
Saved
public int[][] traversals(TreeNode root) {
    // Write your code here.
}
Checking account…