Skip to content

Commit

Permalink
Merge pull request #559 from 0xff-dev/331
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 331
  • Loading branch information
6boris authored Aug 3, 2023
2 parents 806cc3c + 1b4a94e commit 0a7f04f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -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
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +35,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 0a7f04f

Please sign in to comment.