diff --git a/leetcode/901-1000/0965.Univalued-Binary-Tree/README.md b/leetcode/901-1000/0965.Univalued-Binary-Tree/README.md index 7ba028a80..9f888997f 100644 --- a/leetcode/901-1000/0965.Univalued-Binary-Tree/README.md +++ b/leetcode/901-1000/0965.Univalued-Binary-Tree/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/901-1000/0965.Univalued-Binary-Tree/Solution.go b/leetcode/901-1000/0965.Univalued-Binary-Tree/Solution.go index d115ccf5e..80958c583 100644 --- a/leetcode/901-1000/0965.Univalued-Binary-Tree/Solution.go +++ b/leetcode/901-1000/0965.Univalued-Binary-Tree/Solution.go @@ -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 } diff --git a/leetcode/901-1000/0965.Univalued-Binary-Tree/Solution_test.go b/leetcode/901-1000/0965.Univalued-Binary-Tree/Solution_test.go index 14ff50eb4..12ffd7298 100644 --- a/leetcode/901-1000/0965.Univalued-Binary-Tree/Solution_test.go +++ b/leetcode/901-1000/0965.Univalued-Binary-Tree/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +37,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { } diff --git a/leetcode/901-1000/0965.Univalued-Binary-Tree/unival_bst_1.png b/leetcode/901-1000/0965.Univalued-Binary-Tree/unival_bst_1.png new file mode 100644 index 000000000..155325f2b Binary files /dev/null and b/leetcode/901-1000/0965.Univalued-Binary-Tree/unival_bst_1.png differ diff --git a/leetcode/901-1000/0965.Univalued-Binary-Tree/unival_bst_2.png b/leetcode/901-1000/0965.Univalued-Binary-Tree/unival_bst_2.png new file mode 100644 index 000000000..c7073b651 Binary files /dev/null and b/leetcode/901-1000/0965.Univalued-Binary-Tree/unival_bst_2.png differ