-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path513-FindBottomLeftTreeValue.go
More file actions
176 lines (162 loc) · 3.94 KB
/
513-FindBottomLeftTreeValue.go
File metadata and controls
176 lines (162 loc) · 3.94 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package main
// 513. Find Bottom Left Tree Value
// Given the root of a binary tree, return the leftmost value in the last row of the tree.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2020/12/14/tree1.jpg" />
// Input: root = [2,1,3]
// Output: 1
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2020/12/14/tree2.jpg" />
// Input: root = [1,2,3,4,null,5,6,null,null,7]
// Output: 7
// Constraints:
// The number of nodes in the tree is in the range [1, 10^4].
// -2^31 <= Node.val <= 2^31 - 1
import "fmt"
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 findBottomLeftValue(root *TreeNode) int {
if root == nil {
return 0
}
res, maxHeight := 0, -1
var dfs func(root *TreeNode, curHeight int)
dfs = func (root *TreeNode, curHeight int) {
if curHeight > maxHeight && root.Left == nil && root.Right == nil { // 到达最深叶子
maxHeight = curHeight
res = root.Val
}
if root.Left != nil {
dfs(root.Left, curHeight + 1)
}
if root.Right != nil {
dfs(root.Right, curHeight + 1)
}
}
dfs(root, 0)
return res
}
// BFS
func findBottomLeftValue1(root *TreeNode) int {
queue := []*TreeNode{root}
for len(queue) > 0 {
next := []*TreeNode{}
for _, node := range queue {
if node.Left != nil {
next = append(next, node.Left)
}
if node.Right != nil {
next = append(next, node.Right)
}
}
if len(next) == 0 {
return queue[0].Val
}
queue = next
}
return 0
}
func main() {
fmt.Println(findBottomLeftValue(
&TreeNode {
1,
&TreeNode{1, nil, nil},
&TreeNode{3, nil, nil},
},
)) // 1
fmt.Println(findBottomLeftValue(
&TreeNode {
1,
&TreeNode {
2,
&TreeNode{4, nil, nil},
nil,
},
&TreeNode {
3,
&TreeNode{
5,
&TreeNode{7, nil, nil},
nil,
},
&TreeNode{6, nil, nil},
},
},
)) // 7
fmt.Println(findBottomLeftValue(
&TreeNode {
1,
&TreeNode {
2,
&TreeNode{4, nil, nil},
nil,
},
&TreeNode {
3,
&TreeNode{
5,
nil,
&TreeNode{7, nil, nil},
},
&TreeNode{6, nil, nil},
},
},
)) // 7
fmt.Println(findBottomLeftValue1(
&TreeNode {
1,
&TreeNode{1, nil, nil},
&TreeNode{3, nil, nil},
},
)) // 1
fmt.Println(findBottomLeftValue1(
&TreeNode {
1,
&TreeNode {
2,
&TreeNode{4, nil, nil},
nil,
},
&TreeNode {
3,
&TreeNode{
5,
&TreeNode{7, nil, nil},
nil,
},
&TreeNode{6, nil, nil},
},
},
)) // 7
fmt.Println(findBottomLeftValue1(
&TreeNode {
1,
&TreeNode {
2,
&TreeNode{4, nil, nil},
nil,
},
&TreeNode {
3,
&TreeNode{
5,
nil,
&TreeNode{7, nil, nil},
},
&TreeNode{6, nil, nil},
},
},
)) // 7
}