Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-106

Serialize Binary Tree

Problem

Serialize a binary tree into a preorder list with explicit None markers.

tree is a LeetCode-style level-order list with None marking missing nodes. Visit the root, then the left subtree, then the right subtree, appending None whenever a child is missing. An empty tree serializes to [].

Return the preorder serialization.

Hint

Push the right child before the left so the left subtree is processed first.

Test cases

case 1
in: [[1,2,3]]
expected: [1,2,null,null,3,null,null]
case 2
in: [[1]]
expected: [1,null,null]
case 3
in: [[]]
expected: []
case 4
in: [[1,null,2]]
expected: [1,null,2,null,null]
Starter codePython
def serialize_tree(tree):
    # Your code here
    pass

4 test cases · run your code against them in the editor