-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path742-ClosestLeafInABinaryTree.go
More file actions
187 lines (172 loc) · 6.5 KB
/
742-ClosestLeafInABinaryTree.go
File metadata and controls
187 lines (172 loc) · 6.5 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
177
178
179
180
181
182
183
184
185
186
187
package main
// 742. Closest Leaf in a Binary Tree
// Given the root of a binary tree where every node has a unique value and a target integer k,
// return the value of the nearest leaf node to the target k in the tree.
// Nearest to a leaf means the least number of edges traveled on the binary tree to reach any leaf of the tree.
// Also, a node is called a leaf if it has no children.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2021/06/13/closest1-tree.jpg" />
// Input: root = [1,3,2], k = 1
// Output: 2
// Explanation: Either 2 or 3 is the nearest leaf node to the target of 1.
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2021/06/13/closest2-tree.jpg" />
// Input: root = [1], k = 1
// Output: 1
// Explanation: The nearest leaf node is the root node itself.
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2021/06/13/closest3-tree.jpg" />
// Input: root = [1,2,3,4,null,null,null,5,null,6], k = 2
// Output: 3
// Explanation: The leaf node with value 3 (and not the leaf node with value 6) is nearest to the node with value 2.
// Constraints:
// The number of nodes in the tree is in the range [1, 1000].
// 1 <= Node.val <= 1000
// All the values of the tree are unique.
// There exist some node in the tree where Node.val == k.
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 findClosestLeaf(root *TreeNode, k int) int {
res, deep, pre := 0, 1001, []*TreeNode{}
var findK func(*TreeNode, int)
findK = func(root *TreeNode, k int) {
var pLast *TreeNode
nd := root
for nd != nil {
pre = append(pre, nd)
nd = nd.Left
}
for len(pre) > 0 {
nd = pre[len(pre)-1]
if nd.Right == nil || nd.Right == pLast {
if nd.Val == k { return }
pLast = nd
pre = pre[:len(pre)-1]
} else {
nd = nd.Right
for nd != nil {
pre = append(pre, nd)
nd = nd.Left
}
}
}
}
// find k node and its ancestor
findK(root, k)
var findTarget func(*TreeNode, int)
findTarget = func(nd *TreeNode, level int) {
if nd == nil { return }
if nd.Left == nil && nd.Right == nil && level < deep {
res = nd.Val
deep = level
}
findTarget(nd.Left, level+1)
findTarget(nd.Right, level+1)
}
for i := len(pre)-1; i >= 0; i-- {
findTarget(pre[i], len(pre)-1-i)
}
return res
}
func findClosestLeaf1(root *TreeNode, k int) int {
parentMap := make(map[*TreeNode]*TreeNode) // 找到目标节点并构建所需的子节点到父节点的映射
// 遍历函数,找到值为 k 的那个节点,同时记录子节点到父节点的映射
var traverse func(root *TreeNode, k int, parent *TreeNode, parentMap map[*TreeNode]*TreeNode) *TreeNode
traverse = func (root *TreeNode, k int, parent *TreeNode, parentMap map[*TreeNode]*TreeNode) *TreeNode {
if root == nil { return nil }
parentMap[root] = parent // 记录子节点到父节点的映射
if root.Val == k { return root }
left := traverse(root.Left, k, root, parentMap)
if left != nil { return left }
return traverse(root.Right, k, root, parentMap)
}
var bfs func(target *TreeNode, parentMap map[*TreeNode]*TreeNode) int
bfs = func(target *TreeNode, parentMap map[*TreeNode]*TreeNode) int {
queue, visited := []*TreeNode{target}, make(map[*TreeNode]bool)
visited[target] = true
for len(queue) > 0 {
size := len(queue)
for i := 0; i < size; i++ {
cur := queue[0]
queue = queue[1:]
if cur.Left == nil && cur.Right == nil { // 首次到达的叶子结点就是最近的叶子结点
return cur.Val
}
if cur.Left != nil && !visited[cur.Left] {
queue = append(queue, cur.Left)
visited[cur.Left] = true
}
if cur.Right != nil && !visited[cur.Right] {
queue = append(queue, cur.Right)
visited[cur.Right] = true
}
parentNode := parentMap[cur]
if parentNode != nil && !visited[parentNode] {
queue = append(queue, parentNode)
visited[parentNode] = true
}
}
}
return -1
}
target := traverse(root, k, nil, parentMap)
return bfs(target, parentMap)
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2021/06/13/closest1-tree.jpg" />
// Input: root = [1,3,2], k = 1
// Output: 2
// Explanation: Either 2 or 3 is the nearest leaf node to the target of 1.
tree1 := &TreeNode{
1,
&TreeNode{3, nil, nil, },
&TreeNode{2, nil, nil, },
}
fmt.Println(findClosestLeaf(tree1,1)) // 2 | 3
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2021/06/13/closest2-tree.jpg" />
// Input: root = [1], k = 1
// Output: 1
// Explanation: The nearest leaf node is the root node itself.
tree2 := &TreeNode{1, nil, nil, }
fmt.Println(findClosestLeaf(tree2,1)) // 1
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2021/06/13/closest3-tree.jpg" />
// Input: root = [1,2,3,4,null,null,null,5,null,6], k = 2
// Output: 3
// Explanation: The leaf node with value 3 (and not the leaf node with value 6) is nearest to the node with value 2.
tree3 := &TreeNode{
1,
&TreeNode{2, &TreeNode{4, &TreeNode{5, &TreeNode{ 6, nil, nil, }, nil, }, nil, }, nil, },
&TreeNode{3, nil, nil, },
}
fmt.Println(findClosestLeaf(tree3,2)) // 3
tree11 := &TreeNode{
1,
&TreeNode{3, nil, nil, },
&TreeNode{2, nil, nil, },
}
fmt.Println(findClosestLeaf1(tree11,1)) // 2
tree12 := &TreeNode{1, nil, nil, }
fmt.Println(findClosestLeaf1(tree12,1)) // 1
tree13 := &TreeNode{
1,
&TreeNode{2, &TreeNode{4, &TreeNode{5, &TreeNode{ 6, nil, nil, }, nil, }, nil, }, nil, },
&TreeNode{3, nil, nil, },
}
fmt.Println(findClosestLeaf1(tree13,2)) // 3
}