go: add «1110. Delete Nodes And Return Forest»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-07-17 09:25:50 +02:00
parent 33e47203c6
commit c70aba338e
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,38 @@
package main
import "slices"
func delNodes(root *TreeNode, to_delete []int) []*TreeNode {
var forest []*TreeNode
var delNodesRec func(*TreeNode) *TreeNode
delNodesRec = func(node *TreeNode) *TreeNode {
if node == nil {
return nil
}
node.Left = delNodesRec(node.Left)
node.Right = delNodesRec(node.Right)
if slices.Contains(to_delete, node.Val) {
if node.Left != nil {
forest = append(forest, node.Left)
}
if node.Right != nil {
forest = append(forest, node.Right)
}
return nil
}
return node
}
root = delNodesRec(root)
if root != nil {
forest = append(forest, root)
}
return forest
}