java.lang.Object
g0301_0400.s0331_verify_preorder_serialization_of_a_binary_tree.Solution

public class Solution extends Object
331 - Verify Preorder Serialization of a Binary Tree.<p>Medium</p> <p>One way to serialize a binary tree is to use <strong>preorder traversal</strong>. When we encounter a non-null node, we record the node&rsquo;s value. If it is a null node, we record using a sentinel value such as <code>'#'</code>.</p> <p><img src="https://assets.leetcode.com/uploads/2021/03/12/pre-tree.jpg" alt="" /></p> <p>For example, the above binary tree can be serialized to the string <code>&quot;9,3,4,#,#,1,#,#,2,#,6,#,#&quot;</code>, where <code>'#'</code> represents a null node.</p> <p>Given a string of comma-separated values <code>preorder</code>, return <code>true</code> if it is a correct preorder traversal serialization of a binary tree.</p> <p>It is <strong>guaranteed</strong> that each comma-separated value in the string must be either an integer or a character <code>'#'</code> representing null pointer.</p> <p>You may assume that the input format is always valid.</p> <ul> <li>For example, it could never contain two consecutive commas, such as <code>&quot;1,,3&quot;</code>.</li> </ul> <p>**Note: **You are not allowed to reconstruct the tree.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> preorder = &ldquo;9,3,4,#,#,1,#,#,2,#,6,#,#&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> preorder = &ldquo;1,#&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> preorder = &ldquo;9,#,#,1&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= preorder.length <= 10<sup>4</sup></code></li> <li><code>preorder</code> consist of integers in the range <code>[0, 100]</code> and <code>'#'</code> separated by commas <code>','</code>.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • isValidSerialization

      public boolean isValidSerialization(String preorder)