-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path687-LongestUnivaluePath.go
More file actions
146 lines (135 loc) · 4 KB
/
687-LongestUnivaluePath.go
File metadata and controls
146 lines (135 loc) · 4 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
140
141
142
143
144
145
146
package main
// 687. Longest Univalue Path
// Given the root of a binary tree, return the length of the longest path, where each node in the path has the same value.
// This path may or may not pass through the root.
// The length of the path between two nodes is represented by the number of edges between them.
// Example 1:
// (5)
// / \
// 4 (5)
// / \ \
// 4 4 (5)
// <img src="https://assets.leetcode.com/uploads/2020/10/13/ex1.jpg" />
// Input: root = [5,4,5,1,1,null,5]
// Output: 2
// Explanation: The shown image shows that the longest path of the same value (i.e. 5).
// Example 2:
// 1
// / \
// (4) 5
// / \ \
// (4) (4) 5
// <img src="https://assets.leetcode.com/uploads/2020/10/13/ex2.jpg" />
// Input: root = [1,4,5,4,4,null,5]
// Output: 2
// Explanation: The shown image shows that the longest path of the same value (i.e. 4).
// Constraints:
// The number of nodes in the tree is in the range [0, 10^4].
// -1000 <= Node.val <= 1000
// The depth of the tree will not exceed 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
* }
*/
// dfs
func longestUnivaluePath(root *TreeNode) int {
if root == nil {
return 0
}
res := 0
var dfs func(node *TreeNode) int
dfs = func(node *TreeNode) int {
v, sum := 0, 0
if node.Left != nil {
vl := dfs(node.Left)
if node.Val == node.Left.Val {
v = vl + 1
sum += vl + 1
}
}
if node.Right != nil {
vr := dfs(node.Right)
if node.Val == node.Right.Val {
if vr + 1 > v {
v = vr + 1
}
sum += vr + 1
}
}
if sum > res {
res = sum
}
return v
}
dfs(root)
return res
}
func longestUnivaluePath1(root *TreeNode) int {
res := 0
max := func (x, y int) int { if x > y { return x; }; return y; }
var dfs func(node *TreeNode) int
dfs = func(node *TreeNode) int{
if node == nil {
return -1
}
left := dfs(node.Left) + 1
right := dfs(node.Right) + 1
if node.Left != nil && node.Left.Val != node.Val {
left = 0
}
if node.Right != nil && node.Right.Val != node.Val {
right = 0
}
res = max(res,left + right)
return max(left,right)
}
dfs(root)
return res
}
func main() {
// Example 1:
// (5)
// / \
// 4 (5)
// / \ \
// 4 4 (5)
// <img src="https://assets.leetcode.com/uploads/2020/10/13/ex1.jpg" />
// Input: root = [5,4,5,1,1,null,5]
// Output: 2
// Explanation: The shown image shows that the longest path of the same value (i.e. 5).
tree1 := &TreeNode {
5,
&TreeNode{4, &TreeNode{4, nil, nil, }, &TreeNode{4, nil, nil, }, },
&TreeNode{5, nil, &TreeNode{5, nil, nil, }, },
}
fmt.Println(longestUnivaluePath(tree1)) // 2
// Example 2:
// 1
// / \
// (4) 5
// / \ \
// (4) (4) 5
// <img src="https://assets.leetcode.com/uploads/2020/10/13/ex2.jpg" />
// Input: root = [1,4,5,4,4,null,5]
// Output: 2
// Explanation: The shown image shows that the longest path of the same value (i.e. 4).
tree2 := &TreeNode {
1,
&TreeNode{4, &TreeNode{4, nil, nil, }, &TreeNode{4, nil, nil, }, },
&TreeNode{5, nil, &TreeNode{5, nil, nil, }, },
}
fmt.Println(longestUnivaluePath(tree2)) // 2
fmt.Println(longestUnivaluePath1(tree1)) // 2
fmt.Println(longestUnivaluePath1(tree2)) // 2
}