Verify Preorder Serialization

Medium

Description

Given a string of comma-separated values representing preorder traversal with # for null, verify if it represents a valid binary tree serialization.

Examples

Input:preorder = "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output:true
Explanation:

Valid serialization.

Input:preorder = "1, arg2 = #"
Output:false
Explanation:

For preorder = "1, arg2 = #", the answer is false because the verify preorder serialization condition is not met.

Input:preorder = "#"
Output:
Explanation:

A single null node '#' is a valid preorder serialization representing an empty tree. This is the simplest valid case where the tree has no actual nodes, just a null marker.

Constraints

  • 1 ≤ preorder.length ≤ 10⁴

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!