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
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.
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
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)
โ Linear Growth
Space Complexity
O(n)
Hash map stores at most n nodes, set stores at most n children
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code