Skip to content

Practice Construct String from Binary Tree

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#606
LeetCode Problem

Construct String from Binary Tree

Problem Statement

Serialize the binary tree in preorder using each node's integer value. Put the left-child representation in parentheses immediately after its parent, followed by the right-child representation in another pair. Omit parentheses for absent children, except that a node with a right child but no left child must include () before the right-child parentheses. Leaves contain only their value. Return the string without spaces.

Source: Construct String from Binary Tree on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
root = [1,2,3,4]
Output
"1(2(4))(3)"

# Example 2

Input
root = [1,2,3,null,4]
Output
"1(2()(4))(3)"

Constraints

The number of nodes in the tree is in the range [1, 10^4].
-1000 <= Node.val <= 1000

Related LeetCode Problems

Practice more problems similar to Construct String from Binary Tree.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.