2dbi
Home/Fastly/Generate a String From Binary Tree Node Values
FFastly·DSASWE-2Technical Phone Screen

Generate a String From Binary Tree Node Values

Problem

Given a binary tree, build a string encoding its structure using node values and parentheses for children.

Example

[1,2,3,null,4] -> "1(2()(4))(3)"

Constraints

  • Preorder with parentheses; omit empty trailing parens

Approach

Preorder DFS; careful with when to emit '()' for a missing left child. Reported Fastly question.

added 6 days ago
LeadersAccount