Serialize a Binary Search Tree
Updated
Problem statement
You are given the root of a binary search tree with unique integer values. Serialize the tree as its preorder traversal, joining decimal values with commas and using no null markers.
Return the empty string for an empty tree.
As an interview follow-up, explain how to deserialize this representation in linear time by consuming preorder values under valid lower and upper bounds, and how to produce the preorder iteratively for a very deep tree.
Function
serializeBST(root: TreeNode) → StringExamples
Example 1
root = [2,1,3]return = "2,1,3"Preorder visits the root, then the left subtree, then the right subtree.
Example 2
root = [8,3,10,1,6,null,14,null,null,4,7,13]return = "8,3,1,6,4,7,10,14,13"The output lists exactly the preorder traversal and does not emit null markers.
Example 3
root = []return = ""An empty tree serializes to the empty string.
Constraints
- The tree contains at most
100000nodes. -1000000000 <= node.val <= 1000000000.- All values are unique and satisfy the binary-search-tree invariant.
More Confluent Onsite Interview questions
- Implement tail -nPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sudoku Board ValidationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sudoku Puzzle SolverPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Sequential Phrase Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Bounded Combination Sum with Signed CandidatesONSITE INTERVIEW · Seen Oct 2026
- Detect a Silent SensorONSITE INTERVIEW · Seen Sep 2026
- Build an Inverted Document IndexONSITE INTERVIEW · Seen Sep 2026
- Combination Sum with Unlimited ReuseONSITE INTERVIEW · Seen Sep 2026
- Word Search Across DocumentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Windowed Key-Value MapPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2025
Similar questions at other companies
- Filesystem Entity Total SizeGoogle · ONSITE INTERVIEWGoogle interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Kth Smallest Element in a Binary Search TreeSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEWAMD interview questions
- Compare N-Ary Tree Leaf SequencesNavan · ONSITE INTERVIEWNavan interview questions
- Flatten JSON into Repeated Key-Value PairsKlaviyo · ONSITE INTERVIEWKlaviyo interview questions
- Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
- Validate Binary Search TreeInMobi · ONSITE INTERVIEWInMobi interview questions