Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 965 #916

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions leetcode/901-1000/0965.Univalued-Binary-Tree/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
# [965.Univalued 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
A binary tree is **uni-valued** if every node in the tree has the same value.

Given the `root` of a binary tree, return `true` if the given tree is **uni-valued**, or `false` otherwise.

**Example 1:**

**Example 1:**
![1](./unival_bst_1.png)

```
Input: a = "11", b = "1"
Output: "100"
Input: root = [1,1,1,1,1,null,1]
Output: true
```

## 题意
> ...
**Example 2:**

## 题解
![2](./unival_bst_2.png)

### 思路1
> ...
Univalued Binary Tree
```go
```

Input: root = [2,2,2,5,2]
Output: false
```

## 结语

Expand Down
25 changes: 23 additions & 2 deletions leetcode/901-1000/0965.Univalued-Binary-Tree/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package Solution

func Solution(x bool) bool {
return x
type TreeNode struct {
Val int
Left, Right *TreeNode
}

func Solution(root *TreeNode) bool {
if root == nil {
return true
}
ok := true
if root.Left != nil {
if root.Val != root.Left.Val {
return false
}
ok = ok && Solution(root.Left)
}
if root.Right != nil {
if root.Val != root.Right.Val {
return false
}
ok = ok && Solution(root.Right)
}
return ok
}
19 changes: 13 additions & 6 deletions leetcode/901-1000/0965.Univalued-Binary-Tree/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,19 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
inputs *TreeNode
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", &TreeNode{
Val: 1,
Left: &TreeNode{Val: 1, Left: &TreeNode{Val: 1}, Right: &TreeNode{Val: 1}},
Right: &TreeNode{Val: 1, Right: &TreeNode{Val: 1}},
}, true},
{"TestCase", &TreeNode{
Val: 2,
Left: &TreeNode{Val: 2, Left: &TreeNode{Val: 5}, Right: &TreeNode{Val: 2}},
Right: &TreeNode{Val: 2},
}, false},
}

// 开始测试
Expand All @@ -30,10 +37,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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading