-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1110.Delete-Nodes-And-Return-Forest.go
More file actions
52 lines (48 loc) · 1.08 KB
/
1110.Delete-Nodes-And-Return-Forest.go
File metadata and controls
52 lines (48 loc) · 1.08 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
/*
* @lc app=leetcode id=1110 lang=golang
*
* [1110] Delete Nodes And Return Forest
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// 12 ms, faster than 95.87%
// [1,2,4,null,3]\n[3]
// var ans = []*TreeNode{} // 全局变量作为最终的返回
func delNodes(root *TreeNode, to_delete []int) []*TreeNode {
dels := make(map[int]bool, len(to_delete))
for _, v := range to_delete {
dels[v] = true
}
ans := []*TreeNode{}
// del(root, dels) 典型错误,这样会少算当前树的根节点
// return ans
root = del(root, dels, &ans)
if root != nil {
ans = append(ans, root)
}
return ans
}
func del(root *TreeNode, dels map[int]bool, ans *([]*TreeNode)) *TreeNode {
if root == nil {
return root
}
// left,right,root 后序
root.Left = del(root.Left, dels, ans)
root.Right = del(root.Right, dels, ans)
if dels[root.Val] == true {
if root.Left != nil {
*ans = append(*ans, root.Left)
}
if root.Right != nil {
*ans = append(*ans, root.Right)
}
return nil
}
return root
}