Problem · Tree

Create a Binary Tree from Parent-Child Descriptions

MediumAmazon logoAmazonNEW GRADPHONE SCREEN
See Amazon hiring insights

Problem statement

You are given an array descriptions. Each entry has the form [parent, child, isLeft] and describes one edge in a binary tree:

  • If isLeft is 1, child is the left child of parent.
  • If isLeft is 0, child is the right child of parent.

The descriptions may appear in any order, so an entry can mention a child before the entry that connects its parent to the rest of the tree.

The problem statement continues
Pro

Examples

Example 1

descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]]return = [50,20,80,15,17,19]

50 is the only value that never appears as a child. Its children are 20 and 80; the remaining descriptions attach 15, 17, and 19.

FastPrep Pro
Reported in 1 Amazon interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 1 more worked example, explained
  • Guided hints and editorial
  • Run your code on real test cases
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Amazon interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 1 more worked example, explained
  • Guided hints and editorial
  • Run your code on real test cases
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week