Problem · Tree
Create a Binary Tree from Parent-Child Descriptions
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
isLeftis1,childis the left child ofparent. - If
isLeftis0,childis the right child ofparent.
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
ProExamples
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 weekUnlock 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