#331
Verify Preorder Serialization of a Binary Tree
specialist · 910 · lc medium +31 · verified · 47.1% accepted · 2,453 likes · top 32%
Description
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
Code
1
2
3