Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Deserialize a binary tree

Reconstruct a binary tree from its level-order string representation.

Python practice19 minTrees & Binary TreesIntermediateLast updated March 29, 2026

Problem statement

Given a string produced by the serialize function (level-order, comma-separated tokens, 'null' for missing nodes), implement deserialize(data) that reconstructs and returns the original binary tree root. An empty string corresponds to an empty tree (return None).

Task

Implement deserialize to parse the serialization format and rebuild the original binary tree structure.

Examples

Deserialize example

Input

'1,2,3,null,4,5'

Output

[1, 2, 3, None, 4, 5]

Reconstruct the tree and represent it as a level-order list with None for missing children.

Input format

A string encoding a tree produced by serialize (or an empty string).

Output format

A TreeNode root. For testing, use tree_to_list(root) to get a comparable list representation.

Constraints

Input tokens are integers or 'null'. The tree can be empty. Aim for O(n) time.

Samples

Sample 1

Input

'1,2,3,4,5,6,7'

Output

[1, 2, 3, 4, 5, 6, 7]

Perfect binary tree reconstructs with all nodes present.