Encode N-ary Tree to Binary Tree - Problem
Design an Algorithm to Transform N-ary Trees into Binary Trees
You're tasked with creating a reversible transformation system that can convert any N-ary tree (where nodes can have unlimited children) into a binary tree (where nodes have at most 2 children), and then convert it back to the original structure.
๐ณ The Challenge: An N-ary tree allows nodes to have any number of children, while binary trees restrict each node to at most 2 children. Your encoding must preserve all structural information so that decoding produces the exact original tree.
Input: Root of an N-ary tree where each node has a value and a list of children
Output: A pair of functions -
Example: If you have a tree with root 1 having children [2,3,4], your encoding should create a binary tree that can be decoded back to this exact structure.
๐ก Key Insight: You have complete freedom in how you structure the binary tree representation - be creative!
You're tasked with creating a reversible transformation system that can convert any N-ary tree (where nodes can have unlimited children) into a binary tree (where nodes have at most 2 children), and then convert it back to the original structure.
๐ณ The Challenge: An N-ary tree allows nodes to have any number of children, while binary trees restrict each node to at most 2 children. Your encoding must preserve all structural information so that decoding produces the exact original tree.
Input: Root of an N-ary tree where each node has a value and a list of children
Output: A pair of functions -
encode() that converts N-ary to binary tree, and decode() that converts back to N-ary treeExample: If you have a tree with root 1 having children [2,3,4], your encoding should create a binary tree that can be decoded back to this exact structure.
๐ก Key Insight: You have complete freedom in how you structure the binary tree representation - be creative!
Input & Output
example_1.py โ Basic N-ary Tree
$
Input:
Root = 1, children = [3, 2, 4]
โบ
Output:
Binary tree with 1 as root, left=3, and 3.right=2, 2.right=4
๐ก Note:
The N-ary tree with root 1 having three children [3,2,4] gets encoded where node 1's left points to first child (3), and the siblings 2 and 4 are chained via right pointers: 3โ2โ4
example_2.py โ Nested Children
$
Input:
Root = 1, children = [3, 2, 4], where 3 has children = [5, 6]
โบ
Output:
Binary tree preserving all relationships through first-child-next-sibling encoding
๐ก Note:
Node 3's children [5,6] are encoded as 3.left=5 and 5.right=6, while maintaining the sibling chain 3โ2โ4 at the upper level. The pattern applies recursively at each level.
example_3.py โ Single Node
$
Input:
Root = 1, children = []
โบ
Output:
Binary tree with single node 1, left=null, right=null
๐ก Note:
Edge case where N-ary tree has no children results in a binary tree node with no left or right children, demonstrating the encoding handles leaf nodes correctly.
Constraints
- The height of the n-ary tree is less than or equal to 1000
- The total number of nodes is between [0, 104]
- Do not use class member/global/static variables to store states
- Your encode and decode algorithms should be stateless
Visualization
Tap to expand
Understanding the Visualization
1
Original N-ary Structure
Each node can have unlimited children arranged in a list
2
Apply Binary Constraint
Each node now has only two pointers: left and right
3
Left = First Child
The left pointer always points to the first child in the original list
4
Right = Next Sibling
The right pointer chains siblings together horizontally
5
Recursive Application
This pattern applies to every node in the tree, preserving all relationships
Key Takeaway
๐ฏ Key Insight: The first-child-next-sibling pattern is a classic technique that elegantly converts any tree structure into binary format while preserving all relationships. This approach is optimal in both time O(n) and space O(h) complexity.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code