331. Verify Preorder Serialization of a Binary Tree Medium

@problem@discussion
#String#Stack#Tree#Binary Tree



1/**
2 * [331] Verify Preorder Serialization of a Binary Tree
3 *
4 * One way to serialize a binary tree is to use preorder traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as '#'.
5 * <img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/pre-tree.jpg" style="width: 362px; height: 293px;" />
6 * For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where '#' represents a null node.
7 * Given a string of comma-separated values preorder, return true if it is a correct preorder traversal serialization of a binary tree.
8 * It is guaranteed that each comma-separated value in the string must be either an integer or a character '#' representing null pointer.
9 * You may assume that the input format is always valid.
10 * 
11 * 	For example, it could never contain two consecutive commas, such as "1,,3".
12 * 
13 * Note: You are not allowed to reconstruct the tree.
14 *  
15 * Example 1:
16 * Input: preorder = "9,3,4,#,#,1,#,#,2,#,6,#,#"
17 * Output: true
18 * Example 2:
19 * Input: preorder = "1,#"
20 * Output: false
21 * Example 3:
22 * Input: preorder = "9,#,#,1"
23 * Output: false
24 *  
25 * Constraints:
26 * 
27 * 	1 <= preorder.length <= 10^4
28 * 	preorder consist of integers in the range [0, 100] and '#' separated by commas ','.
29 * 
30 */
31pub struct Solution {}
32
33// problem: https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/
34// discuss: https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/discuss/?currentPage=1&orderBy=most_votes&query=
35
36// submission codes start here
37
38impl Solution {
39    pub fn is_valid_serialization(preorder: String) -> bool {
40        false
41    }
42}
43
44// submission codes end
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49
50    #[test]
51    fn test_331() {
52    }
53}
54


Back
© 2025 bowen.ge All Rights Reserved.