Create Binary Tree From Descriptions - Problem
Create Binary Tree From Descriptions

You're given a collection of parent-child relationships in a binary tree and need to reconstruct the complete tree structure. Each relationship is described as [parent, child, isLeft] where:

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

Your task is to build the binary tree from these descriptions and return its root node. Think of it like assembling a family tree from scattered birth certificates - you need to piece together all the relationships to form the complete tree structure.

Goal: Construct and return the root of the binary tree described by the given parent-child relationships.

Input & Output

example_1.py โ€” Basic Tree
$ Input: descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]]
โ€บ Output: Tree with root node 50
๐Ÿ’ก Note: 50 is the root (appears as parent but never as child). 50 has left child 20 and right child 80. 20 has left child 15 and right child 17. 80 has left child 19.
example_2.py โ€” Simple Tree
$ Input: descriptions = [[1,2,1],[2,3,0],[3,4,1]]
โ€บ Output: Tree with root node 1
๐Ÿ’ก Note: Linear tree structure: 1 is root with left child 2, 2 has right child 3, 3 has left child 4.
example_3.py โ€” Single Relationship
$ Input: descriptions = [[1,2,1]]
โ€บ Output: Tree with root node 1
๐Ÿ’ก Note: Minimal tree with just root 1 and its left child 2.

Visualization

Tap to expand
๐ŸŒณ Family Tree Construction๐Ÿ“‹ Birth Certificates[John, Alice, left_child][John, Bob, right_child][Alice, Charlie, left_child]๐Ÿ—‚๏ธ Filing SystemJohnAliceBobCharlie๐ŸŒณ Family TreeJohnAliceBobCharlie๐Ÿ” Key Insight: Finding the Root๐Ÿ’ก The root ancestor is the person who appears as a PARENT in certificatesbut NEVER appears as a CHILD in any certificate!ParentsJohn, AliceChildrenAlice, Bob, Charlie๐ŸŽฏ ROOTJohn (Parent only!)
Understanding the Visualization
1
Set Up Organization System
Create a filing system (hash map) to store family members and a notebook (set) to track who are children
2
Process Each Certificate
For each birth certificate, create person records if they don't exist, then connect the parent-child relationship
3
Identify the Patriarch/Matriarch
The root of the family tree is the person who appears as a parent but never appears as anyone's child
Key Takeaway
๐ŸŽฏ Key Insight: Use a hash map for O(1) node creation and lookup, track children in a set, then find the root as the node that's a parent but never a child - all in a single pass!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through descriptions array, hash map operations are O(1)

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Hash map stores at most n nodes, set stores at most n children

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค descriptions.length โ‰ค 104
  • descriptions[i].length == 3
  • 1 โ‰ค parenti, childi โ‰ค 105
  • parenti != childi
  • The binary tree described by descriptions is valid
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
62.3K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen