Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 53e6b4a

Browse files
committed
965 finish
1 parent ddbcc32 commit 53e6b4a

File tree

7 files changed

+125
-32
lines changed

7 files changed

+125
-32
lines changed

.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
"true",
112112
"ttca",
113113
"unique",
114+
"univalued",
114115
"username",
115116
"uvwxyz",
116117
"worldabcefghijkmnpqstuvxyz",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [965. Univalued Binary Tree](https://leetcode.com/problems/univalued-binary-tree/)
2+
3+
A binary tree is univalued if every node in the tree has the same value.
4+
5+
Return true if and only if the given tree is univalued.
6+
7+
Example 1:
8+
9+
![ex1](ex1.png)
10+
11+
```text
12+
Input: [1,1,1,1,1,null,1]
13+
Output: true
14+
```
15+
16+
Example 2:
17+
18+
![ex2](ex2.png)
19+
20+
```text
21+
Input: [2,2,2,5,2]
22+
Output: false
23+
```
24+
25+
Note:
26+
27+
1. The number of nodes in the given tree will be in the range [1, 100].
28+
1. Each node's value will be an integer in the range [0, 99].
6.8 KB
Loading
6.99 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package problem0965
2+
3+
import "github.com/aQuaYi/LeetCode-in-Go/kit"
4+
5+
// TreeNode is pre-defined in kit
6+
type TreeNode = kit.TreeNode
7+
8+
func isUnivalTree(root *TreeNode) bool {
9+
if root == nil {
10+
return true
11+
}
12+
13+
if (root.Left != nil && root.Val != root.Left.Val) ||
14+
(root.Right != nil && root.Val != root.Right.Val) {
15+
return false
16+
}
17+
18+
return isUnivalTree(root.Left) && isUnivalTree(root.Right)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package problem0965
2+
3+
import (
4+
"testing"
5+
6+
"github.com/aQuaYi/LeetCode-in-Go/kit"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
root []int
13+
ans bool
14+
}{
15+
16+
{
17+
[]int{1, 1, 1, 1, 1, kit.NULL, 1},
18+
true,
19+
},
20+
21+
{
22+
[]int{2, 2, 2, 5, 2},
23+
false,
24+
},
25+
26+
// 可以有多个 testcase
27+
}
28+
29+
func Test_isUnivalTree(t *testing.T) {
30+
ast := assert.New(t)
31+
32+
for _, tc := range tcs {
33+
root := kit.Ints2TreeNode(tc.root)
34+
ast.Equal(tc.ans, isUnivalTree(root), "输入:%v", tc)
35+
}
36+
}
37+
38+
func Benchmark_isUnivalTree(b *testing.B) {
39+
for i := 0; i < b.N; i++ {
40+
for _, tc := range tcs {
41+
root := kit.Ints2TreeNode(tc.root)
42+
isUnivalTree(root)
43+
}
44+
}
45+
}

leetcode.json

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Username": "aQuaYi",
3-
"Ranking": 905,
4-
"Updated": "2019-03-19T21:26:11.323775847+08:00",
3+
"Ranking": 904,
4+
"Updated": "2019-03-21T21:36:11.444943572+08:00",
55
"Record": {
66
"Easy": {
77
"Solved": 214,
@@ -661,7 +661,7 @@
661661
"ID": 53,
662662
"Title": "Maximum Subarray",
663663
"TitleSlug": "maximum-subarray",
664-
"PassRate": "42%",
664+
"PassRate": "43%",
665665
"Difficulty": "Easy",
666666
"IsAccepted": true,
667667
"IsPaid": false,
@@ -1417,7 +1417,7 @@
14171417
"ID": 116,
14181418
"Title": "Populating Next Right Pointers in Each Node",
14191419
"TitleSlug": "populating-next-right-pointers-in-each-node",
1420-
"PassRate": "36%",
1420+
"PassRate": "37%",
14211421
"Difficulty": "Medium",
14221422
"IsAccepted": false,
14231423
"IsPaid": false,
@@ -1717,7 +1717,7 @@
17171717
"ID": 141,
17181718
"Title": "Linked List Cycle",
17191719
"TitleSlug": "linked-list-cycle",
1720-
"PassRate": "35%",
1720+
"PassRate": "36%",
17211721
"Difficulty": "Easy",
17221722
"IsAccepted": false,
17231723
"IsPaid": false,
@@ -3085,7 +3085,7 @@
30853085
"ID": 255,
30863086
"Title": "Verify Preorder Sequence in Binary Search Tree",
30873087
"TitleSlug": "verify-preorder-sequence-in-binary-search-tree",
3088-
"PassRate": "42%",
3088+
"PassRate": "43%",
30893089
"Difficulty": "Medium",
30903090
"IsAccepted": false,
30913091
"IsPaid": true,
@@ -4657,7 +4657,7 @@
46574657
"ID": 386,
46584658
"Title": "Lexicographical Numbers",
46594659
"TitleSlug": "lexicographical-numbers",
4660-
"PassRate": "44%",
4660+
"PassRate": "45%",
46614661
"Difficulty": "Medium",
46624662
"IsAccepted": false,
46634663
"IsPaid": false,
@@ -5149,7 +5149,7 @@
51495149
"ID": 427,
51505150
"Title": "Construct Quad Tree",
51515151
"TitleSlug": "construct-quad-tree",
5152-
"PassRate": "54%",
5152+
"PassRate": "55%",
51535153
"Difficulty": "Easy",
51545154
"IsAccepted": false,
51555155
"IsPaid": false,
@@ -5341,7 +5341,7 @@
53415341
"ID": 443,
53425342
"Title": "String Compression",
53435343
"TitleSlug": "string-compression",
5344-
"PassRate": "36%",
5344+
"PassRate": "37%",
53455345
"Difficulty": "Easy",
53465346
"IsAccepted": true,
53475347
"IsPaid": false,
@@ -5833,7 +5833,7 @@
58335833
"ID": 484,
58345834
"Title": "Find Permutation",
58355835
"TitleSlug": "find-permutation",
5836-
"PassRate": "56%",
5836+
"PassRate": "57%",
58375837
"Difficulty": "Medium",
58385838
"IsAccepted": false,
58395839
"IsPaid": true,
@@ -6037,7 +6037,7 @@
60376037
"ID": 501,
60386038
"Title": "Find Mode in Binary Search Tree",
60396039
"TitleSlug": "find-mode-in-binary-search-tree",
6040-
"PassRate": "38%",
6040+
"PassRate": "39%",
60416041
"Difficulty": "Easy",
60426042
"IsAccepted": true,
60436043
"IsPaid": false,
@@ -6145,7 +6145,7 @@
61456145
"ID": 510,
61466146
"Title": "Inorder Successor in BST II",
61476147
"TitleSlug": "inorder-successor-in-bst-ii",
6148-
"PassRate": "55%",
6148+
"PassRate": "54%",
61496149
"Difficulty": "Medium",
61506150
"IsAccepted": false,
61516151
"IsPaid": true,
@@ -6373,7 +6373,7 @@
63736373
"ID": 529,
63746374
"Title": "Minesweeper",
63756375
"TitleSlug": "minesweeper",
6376-
"PassRate": "51%",
6376+
"PassRate": "52%",
63776377
"Difficulty": "Medium",
63786378
"IsAccepted": true,
63796379
"IsPaid": false,
@@ -6445,7 +6445,7 @@
64456445
"ID": 535,
64466446
"Title": "Encode and Decode TinyURL",
64476447
"TitleSlug": "encode-and-decode-tinyurl",
6448-
"PassRate": "75%",
6448+
"PassRate": "76%",
64496449
"Difficulty": "Medium",
64506450
"IsAccepted": false,
64516451
"IsPaid": false,
@@ -7933,7 +7933,7 @@
79337933
"ID": 659,
79347934
"Title": "Split Array into Consecutive Subsequences",
79357935
"TitleSlug": "split-array-into-consecutive-subsequences",
7936-
"PassRate": "39%",
7936+
"PassRate": "40%",
79377937
"Difficulty": "Medium",
79387938
"IsAccepted": true,
79397939
"IsPaid": false,
@@ -9157,7 +9157,7 @@
91579157
"ID": 761,
91589158
"Title": "Special Binary String",
91599159
"TitleSlug": "special-binary-string",
9160-
"PassRate": "49%",
9160+
"PassRate": "50%",
91619161
"Difficulty": "Hard",
91629162
"IsAccepted": true,
91639163
"IsPaid": false,
@@ -9325,7 +9325,7 @@
93259325
"ID": 775,
93269326
"Title": "Global and Local Inversions",
93279327
"TitleSlug": "global-and-local-inversions",
9328-
"PassRate": "37%",
9328+
"PassRate": "38%",
93299329
"Difficulty": "Medium",
93309330
"IsAccepted": true,
93319331
"IsPaid": false,
@@ -9337,7 +9337,7 @@
93379337
"ID": 776,
93389338
"Title": "Split BST",
93399339
"TitleSlug": "split-bst",
9340-
"PassRate": "51%",
9340+
"PassRate": "52%",
93419341
"Difficulty": "Medium",
93429342
"IsAccepted": false,
93439343
"IsPaid": true,
@@ -9553,7 +9553,7 @@
95539553
"ID": 794,
95549554
"Title": "Valid Tic-Tac-Toe State",
95559555
"TitleSlug": "valid-tic-tac-toe-state",
9556-
"PassRate": "28%",
9556+
"PassRate": "29%",
95579557
"Difficulty": "Medium",
95589558
"IsAccepted": true,
95599559
"IsPaid": false,
@@ -9889,7 +9889,7 @@
98899889
"ID": 822,
98909890
"Title": "Card Flipping Game",
98919891
"TitleSlug": "card-flipping-game",
9892-
"PassRate": "39%",
9892+
"PassRate": "40%",
98939893
"Difficulty": "Medium",
98949894
"IsAccepted": true,
98959895
"IsPaid": false,
@@ -10081,7 +10081,7 @@
1008110081
"ID": 838,
1008210082
"Title": "Push Dominoes",
1008310083
"TitleSlug": "push-dominoes",
10084-
"PassRate": "42%",
10084+
"PassRate": "43%",
1008510085
"Difficulty": "Medium",
1008610086
"IsAccepted": true,
1008710087
"IsPaid": false,
@@ -10861,7 +10861,7 @@
1086110861
"ID": 903,
1086210862
"Title": "Valid Permutations for DI Sequence",
1086310863
"TitleSlug": "valid-permutations-for-di-sequence",
10864-
"PassRate": "41%",
10864+
"PassRate": "42%",
1086510865
"Difficulty": "Hard",
1086610866
"IsAccepted": true,
1086710867
"IsPaid": false,
@@ -10873,7 +10873,7 @@
1087310873
"ID": 904,
1087410874
"Title": "Fruit Into Baskets",
1087510875
"TitleSlug": "fruit-into-baskets",
10876-
"PassRate": "40%",
10876+
"PassRate": "41%",
1087710877
"Difficulty": "Medium",
1087810878
"IsAccepted": true,
1087910879
"IsPaid": false,
@@ -10933,7 +10933,7 @@
1093310933
"ID": 909,
1093410934
"Title": "Snakes and Ladders",
1093510935
"TitleSlug": "snakes-and-ladders",
10936-
"PassRate": "30%",
10936+
"PassRate": "31%",
1093710937
"Difficulty": "Medium",
1093810938
"IsAccepted": true,
1093910939
"IsPaid": false,
@@ -11173,7 +11173,7 @@
1117311173
"ID": 929,
1117411174
"Title": "Unique Email Addresses",
1117511175
"TitleSlug": "unique-email-addresses",
11176-
"PassRate": "75%",
11176+
"PassRate": "74%",
1117711177
"Difficulty": "Easy",
1117811178
"IsAccepted": true,
1117911179
"IsPaid": false,
@@ -11689,7 +11689,7 @@
1168911689
"ID": 972,
1169011690
"Title": "Equal Rational Numbers",
1169111691
"TitleSlug": "equal-rational-numbers",
11692-
"PassRate": "39%",
11692+
"PassRate": "40%",
1169311693
"Difficulty": "Hard",
1169411694
"IsAccepted": false,
1169511695
"IsPaid": false,
@@ -11773,7 +11773,7 @@
1177311773
"ID": 979,
1177411774
"Title": "Distribute Coins in Binary Tree",
1177511775
"TitleSlug": "distribute-coins-in-binary-tree",
11776-
"PassRate": "65%",
11776+
"PassRate": "66%",
1177711777
"Difficulty": "Medium",
1177811778
"IsAccepted": false,
1177911779
"IsPaid": false,
@@ -11869,7 +11869,7 @@
1186911869
"ID": 987,
1187011870
"Title": "Vertical Order Traversal of a Binary Tree",
1187111871
"TitleSlug": "vertical-order-traversal-of-a-binary-tree",
11872-
"PassRate": "33%",
11872+
"PassRate": "32%",
1187311873
"Difficulty": "Medium",
1187411874
"IsAccepted": false,
1187511875
"IsPaid": false,
@@ -11929,7 +11929,7 @@
1192911929
"ID": 992,
1193011930
"Title": "Subarrays with K Different Integers",
1193111931
"TitleSlug": "subarrays-with-k-different-integers",
11932-
"PassRate": "46%",
11932+
"PassRate": "45%",
1193311933
"Difficulty": "Hard",
1193411934
"IsAccepted": false,
1193511935
"IsPaid": false,
@@ -12097,7 +12097,7 @@
1209712097
"ID": 1006,
1209812098
"Title": "Clumsy Factorial",
1209912099
"TitleSlug": "clumsy-factorial",
12100-
"PassRate": "55%",
12100+
"PassRate": "54%",
1210112101
"Difficulty": "Medium",
1210212102
"IsAccepted": false,
1210312103
"IsPaid": false,
@@ -12193,7 +12193,7 @@
1219312193
"ID": 1014,
1219412194
"Title": "Capacity To Ship Packages Within D Days",
1219512195
"TitleSlug": "capacity-to-ship-packages-within-d-days",
12196-
"PassRate": "49%",
12196+
"PassRate": "50%",
1219712197
"Difficulty": "Medium",
1219812198
"IsAccepted": false,
1219912199
"IsPaid": false,
@@ -12205,7 +12205,7 @@
1220512205
"ID": 1015,
1220612206
"Title": "Numbers With Repeated Digits",
1220712207
"TitleSlug": "numbers-with-repeated-digits",
12208-
"PassRate": "31%",
12208+
"PassRate": "32%",
1220912209
"Difficulty": "Hard",
1221012210
"IsAccepted": false,
1221112211
"IsPaid": false,

0 commit comments

Comments
 (0)