Medium

Quiz

#331 Verify Preorder Serialization of a Binary Tree

APPROACH

One way to serialize a binary tree uses preorder traversal, recording node values and using a sentinel value such as '#' for null nodes.

Given a comma-separated string preorder, return true if it is a valid preorder serialization of some binary tree, or false otherwise.

Each token is either an integer or '#'. You are not allowed to reconstruct the tree.

Example 1:

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

Example 2:

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

Example 3:

Input: preorder = "9,#,#,1"
Output: false
1 of 4
1:00

What is the optimal approach for this problem?