diff --git a/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/README.md b/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/README.md index cba1d75a2..f35fd15c3 100644 --- a/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/README.md +++ b/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/README.md @@ -1,28 +1,42 @@ # [331.Verify Preorder Serialization of a Binary Tree][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +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 `'#'`. + +![example](./pre-tree.jpg) + +For example, the above binary tree can be serialized to the string `"9,3,4,#,#,1,#,#,2,#,6,#,#"`, where `'#'` represents a null node. + +Given a string of comma-separated values `preorder`, return `true` if it is a correct preorder traversal serialization of a binary tree. + +It is **guaranteed** that each comma-separated value in the string must be either an integer or a character `'#'` representing null pointer. + +You may assume that the input format is always valid. + +- For example, it could never contain two consecutive commas, such as `"1,,3"`. + +**Note**: You are not allowed to reconstruct the tree. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: preorder = "9,3,4,#,#,1,#,#,2,#,6,#,#" +Output: true ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Verify Preorder Serialization of a Binary Tree -```go +``` +Input: preorder = "1,#" +Output: false ``` +**Example 3:** + +``` +Input: preorder = "9,#,#,1" +Output: false +``` ## 结语 diff --git a/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/Solution.go b/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/Solution.go index d115ccf5e..9870bef6b 100644 --- a/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/Solution.go +++ b/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/Solution.go @@ -1,5 +1,55 @@ package Solution -func Solution(x bool) bool { - return x +import "strings" + +type pair331 struct { + sharp, index int +} + +func Solution(preorder string) bool { + if preorder == "#" { + return true + } + items := strings.Split(preorder, ",") + l := len(items) + if l < 3 { + return false + } + pairs := make([]pair331, 0) + for i := 0; i < l; i++ { + if items[i] != "#" { + pairs = append(pairs, pair331{index: i}) + continue + } + last := len(pairs) - 1 + if last == -1 { + return false + } + if items[pairs[last].index] == "#" || pairs[last].sharp >= 2 { + pairs = append(pairs, pair331{index: i}) + continue + } + pairs[last].sharp++ + } + + stack := []int{-1, -1} + for _, pair := range pairs { + if pair.sharp == 1 { + stack = append(stack, pair.index) + continue + } + if pair.sharp == 0 && items[pair.index] != "#" { + stack = append(stack, pair.index, pair.index) + continue + } + ll := len(stack) + if ll == 0 { + break + } + for ll >= 2 && stack[ll-1] != stack[ll-2] { + ll-- + } + stack = stack[:ll-1] + } + return len(stack) == 1 } diff --git a/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/Solution_test.go b/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/Solution_test.go index 14ff50eb4..c930b7f00 100644 --- a/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/Solution_test.go +++ b/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/Solution_test.go @@ -10,12 +10,17 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs string expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "9,3,4,#,#,1,#,#,2,#,6,#,#", true}, + {"TestCase2", "1,#", false}, + {"TestCase3", "9,3,#,1", false}, + {"TestCase4", "9,#,93,#,9,9,#,#,#", true}, + {"TestCase5", "9,9,91,#,#,9,#,49,#,#,#", true}, + {"TestCase6", "8,#,5,#,2,5,#,7,9,#,8,#,#,#,#", true}, + {"TestCase7", "1,#,#,#,#", false}, + {"TestCase8", "1,#,#", true}, } // 开始测试 @@ -30,10 +35,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/pre-tree.jpg b/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/pre-tree.jpg new file mode 100644 index 000000000..8a501f8d0 Binary files /dev/null and b/leetcode/301-400/0331.Verify-Preorder-Serialization-of-a-Binary-Tree/pre-tree.jpg differ