-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path250-CountUnivalueSubtrees.go
More file actions
139 lines (128 loc) · 3.36 KB
/
250-CountUnivalueSubtrees.go
File metadata and controls
139 lines (128 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
// 250. Count Univalue Subtrees
// Given the root of a binary tree, return the number of uni-value subtrees.
// A uni-value subtree means all nodes of the subtree have the same value.
// Example 1:
// 5
// / \
// 1 (5)
// / \ \
// (5) (5) (5)
// <img src="https://assets.leetcode.com/uploads/2020/08/21/unival_e1.jpg" />
// Input: root = [5,1,5,5,5,null,5]
// Output: 4
// Example 2:
// Input: root = []
// Output: 0
// Example 3:
// (5)
// / \
// (5) (5)
// / \ \
// (5) (5) (5)
// Input: root = [5,5,5,5,5,null,5]
// Output: 6
// Constraints:
// The number of the node in the tree will be in the range [0, 1000].
// -1000 <= Node.val <= 1000
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func countUnivalSubtrees(root *TreeNode) int {
res := 0
var isUnivalSubtrees func(root *TreeNode) bool
isUnivalSubtrees = func (root *TreeNode) bool {
if root == nil {
return true
}
isUnival := true
if root.Left != nil {
if !isUnivalSubtrees(root.Left) || root.Val != root.Left.Val {
isUnival = false
}
}
if root.Right != nil {
if !isUnivalSubtrees(root.Right) || root.Val != root.Right.Val {
isUnival = false
}
}
if isUnival {
res++
}
return isUnival
}
isUnivalSubtrees(root)
return res
}
func countUnivalSubtrees1(root *TreeNode) int {
if root == nil {
return 0
}
res := 0
var dfs func(node *TreeNode, ans *int) (int, bool)
dfs = func(node *TreeNode, ans *int) (int, bool) {
tag := true
if node.Left != nil {
t, tag1 := dfs(node.Left, ans)
if !tag1 || t != node.Val { tag = false; }
}
if node.Right != nil {
t, tag2 := dfs(node.Right, ans)
if !tag2 || t != node.Val { tag = false; }
}
if tag { *ans++; }
return node.Val, tag
}
dfs(root, &res)
return res
}
func main() {
// Example 1:
// 5
// / \
// 1 (5)
// / \ \
// (5) (5) (5)
// <img src="https://assets.leetcode.com/uploads/2020/08/21/unival_e1.jpg" />
// Input: root = [5,1,5,5,5,null,5]
// Output: 4
tree1 := &TreeNode {
5,
&TreeNode{1, &TreeNode{5, nil, nil}, &TreeNode{5, nil, nil}, },
&TreeNode{5, nil, &TreeNode{5, nil, nil}, },
}
fmt.Println(countUnivalSubtrees(tree1)) // 5
// Example 2:
// Input: root = []
// Output: 0
fmt.Println(countUnivalSubtrees(nil)) // 1
// Example 3:
// (5)
// / \
// (5) (5)
// / \ \
// (5) (5) (5)
// Input: root = [5,5,5,5,5,null,5]
// Output: 6
tree3 := &TreeNode {
5,
&TreeNode{5, &TreeNode{5, nil, nil}, &TreeNode{5, nil, nil}, },
&TreeNode{5, nil, &TreeNode{5, nil, nil}, },
}
fmt.Println(countUnivalSubtrees(tree3)) // 6
fmt.Println(countUnivalSubtrees1(tree1)) // 4
fmt.Println(countUnivalSubtrees1(nil)) // 0
fmt.Println(countUnivalSubtrees1(tree3)) // 6
}